[R] Code for Coefficent (Cronbach's) Alpha
Ben Bolker
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
stuff.
-- 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:
> reliability.alpha<-function(x){
>
> score.total<-0
> var.items<-0
> var.total<-0
> z.x<-0
> z.score.total<-0
> z.var.items<-0
> z.x<-scale(x, center = TRUE, scale = TRUE)
> n.items<-length(x)
>
> 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])
>
> var.total<-var(score.total)
> z.var.total<-var(z.score.total)
>
> cronbachs.alpha<-(n.items/(n.items-1))*((var.total -
> var.items)/var.total)
> z.cronbachs.alpha<-(n.items/(n.items-1))*((z.var.total -
> z.var.items)/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
mailing list