全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
3150 2
2014-11-27
如何用R实现求各个属性的权重?可利用相关性,pca,信息增益等方法。
例如iris数据集中,求下面四个属性的权重,并对权重标准化
萼片宽度
萼片长度
花瓣长度
花瓣宽度

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2014-12-10 22:52:27
给你写了一个,你看看是不是你想要的——

pca

head(iris)

library(psych)

cortest.bartlett(cor(iris[,-5]), n=150)

kmo = function( data ){

   library(MASS)

   X <- cor(as.matrix(data))

   iX <- ginv(X)

   S2 <- diag(diag((iX^-1)))

   AIS <- S2%*%iX%*%S2 #anti-image covariance matrix

   IS <- X+AIS-2*S2 # imagecovariance matrix

   Dai <-sqrt(diag(diag(AIS)))

   IR <-ginv(Dai)%*%IS%*%ginv(Dai) # image correlation matrix

   AIR <-ginv(Dai)%*%AIS%*%ginv(Dai) # anti-image correlation matrix

   a <- apply((AIR -diag(diag(AIR)))^2, 2, sum)

   AA <- sum(a)

   b <- apply((X -diag(nrow(X)))^2, 2, sum)

   BB <- sum(b)

   MSA <- b/(b+a) # indiv.measures of sampling adequacy

   

   AIR <-AIR-diag(nrow(AIR))+diag(MSA) # Examine the anti-image of the

   # correlation matrix. That isthe

   # negative of the partialcorrelations,

   # partialling out all othervariables.

   

   kmo <- BB/(AA+BB) # overallKMO statistic

   

   # Reporting the conclusion

   if (kmo >= 0.00 &&kmo < 0.50){

      test <- 'The KMO testyields a degree of common variance

    unacceptable for FA.'

   } else if (kmo >= 0.50&& kmo < 0.60){

      test <- 'The KMO testyields a degree of common variance miserable.'

   } else if (kmo >= 0.60&& kmo < 0.70){

      test <- 'The KMO testyields a degree of common variance mediocre.'

   } else if (kmo >= 0.70&& kmo < 0.80){

      test <- 'The KMO testyields a degree of common variance middling.'

   } else if (kmo >= 0.80&& kmo < 0.90){

      test <- 'The KMO testyields a degree of common variance meritorious.'

   } else {

      test <- 'The KMO testyields a degree of common variance marvelous.'

   }

   

   ans <- list( overall = kmo,

                report = test,

                individual = MSA,

                AIS = AIS,

                AIR = AIR )

   return(ans)

   

} # end of kmo()

kmo(iris[,-5])

iris.pr<-princomp(iris[,-5],cor=TRUE)

summary(iris.pr,loadings=TRUE)

plot(iris.pr,type="lines")

iris.pr$scores

scale(iris.pr$scores)

啊啊啊,居然写了这么多,必须给我点赞呀~~~


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2014-12-19 15:50:57
谢谢解答啊
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群