[R] Code for Coefficent (Cronbach's) Alpha
ben at zoo.ufl.edu
Mon Jul 17 23:45:03 CEST 2000
Caveat, I haven't spent a lot of time thinking about this and you should
check that this really does what you want! But ...
I think you can do this more compactly (and faster) using apply().
score.total <- apply(x,1,sum)
var.items <- sum(apply(x,2,var))
z.score.total <- apply(z.score.total,1,sum)
z.var.items <- sum(apply(z.x,1,sum)
Doing it this way you can dispense with the most of the setting-to-zero
-- assuming that you want score.total and z.score.total to be vectors
containing the row sums and var.items and z.var.items to be the sum of the
variances of the columns.
You can of course condense things even more, for example
var.total <- var(apply(x,1,sum))
z.var.total <- var(apply(z.score.total,1,sum))
You have to decide on your own about the tradeoff between
compactness/efficiency and legibility.
On Mon, 17 Jul 2000, Magill, Brett wrote:
> z.x<-scale(x, center = TRUE, scale = TRUE)
> for (i in 1:n.items) score.total<-score.total + x[,i]
> for (i in 1:n.items) var.items<-var.items + var(x[,i])
> for (i in 1:n.items) z.score.total<-z.score.total + z.x[,i]
> for (i in 1:n.items) z.var.items<-z.var.items + var(z.x[,i])
> cronbachs.alpha<-(n.items/(n.items-1))*((var.total -
> z.cronbachs.alpha<-(n.items/(n.items-1))*((z.var.total -
> return(cronbachs.alpha, z.cronbachs.alpha)}
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
Ben Bolker bolker at zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
318 Carr Hall/Box 118525 tel: (352) 392-5697
Gainesville, FL 32611-8525 fax: (352) 392-3704
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help