Magill, Brett
2001-Mar-26 17:30 UTC
[R] Item Analysis and Cronbach's Alpha (Code Attached)
A short function I wrote for the purpose of evaluating scales made up of a number of questionnaire items. It provides Cronbach's Alpha, both unstandardized and based on standardized items. It also provides item statistics which include item-total correlations (corrected) and item-removed alpha. Thought some of you might find it useful. I would also appreciate any programming tips or suggestions for improving the function as I am still relatively new to R and statistical programming in general. Cheers! Alpha<- function(x){ xmat<-as.matrix(na.omit(x)) xmat.z<-scale(xmat) N<-dim(xmat)[2] Correlation.Matrix<-cor(xmat) Item.Tot.Cor<-rep(NA,N) for (a in 1:N) Item.Tot.Cor[a]<-(cor(apply(xmat[,- a],1,sum),xmat[,a])) Item.Rem.Alpha<-rep(NA,N) for (b in 1:N) Item.Rem.Alpha[b]<- ((N-1)/(N-2)) * (1 -((sum(diag(var(xmat[,-b])))) / (sum(var(xmat[,-b]))))) Item.Mean<-apply(xmat,2,mean) Item.SD<-apply(xmat,2,sd) Item.Var<-apply(xmat,2,var) N.Cases<-rep(dim(xmat)[1],N) ITEM.STATISTICS<-cbind(Item.Mean, Item.Var, Item.SD,Item.Tot.Cor,Item.Rem.Alpha,N.Cases) Cronbachs.Alpha<- (N/(N-1)) * (1 -((sum(diag(var(xmat )))) / (sum(var(xmat ))))) Standardized.Alpha<- (N/(N-1)) * (1 -((sum(diag(var(xmat.z)))) / (sum(var(xmat.z))))) Total.Mean<-mean(apply(xmat,1,sum)) Total.Var<-var(apply(xmat,1,sum)) Total.SD<-sd(apply(xmat,1,sum)) N.Items<-N TOTAL.STATISTICS<-cbind(Cronbachs.Alpha,Standardized.Alpha,Total.Mean,Total. Var,Total.SD, N.Items) row.names(TOTAL.STATISTICS)<-"TOTAL" print("Cronbach's Alpha and Item Analysis") print("----------------------------------") print("Correlation.Matrix") print(Correlation.Matrix,digits=4) print("---------------------------------------------------------") print(ITEM.STATISTICS,digits=4) print("------------------------------------------------------------------") print(TOTAL.STATISTICS,digits=4) } -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Magill, Brett <MagillB at usa.redcross.org> writes:>A short function I wrote for the purpose of evaluating scales made up of a >number of questionnaire items. It provides Cronbach's Alpha, both >unstandardized and based on standardized items. It also provides item >statistics which include item-total correlations (corrected) and >item-removed alpha. Thought some of you might find it useful. I would also >appreciate any programming tips or suggestions for improving the function as >I am still relatively new to R and statistical programming in general.I haven't used the function but one comment is that R functions often return a structure rather than printed output. This allows the use of print() and summary() functions to display different levels of detail as well as allowing the function's output to be paced in an object for further manipulation. This is usually done by returning a list with a class. If you return the list with class "alpha": class(TOTAL.STATISTICS) <- "alpha" return(TOTAL.STATISTICS) You can then create a function print.alpha() which will be called automatically whenever you run alpha() without assigning it's output to another object, when you type the name of an "alpha" object, or use the print() function with an "alpha" object. You could also create a function alpha.summary() which will be called whenever you use the summary() function with an "alpha" object. The print() function usually return a summary and the summary() function returns more detail (I've never worked that out!). There is a brief tutorial on this sort of thing in my notes on R at: http://www.myatt.demon.co.uk Mark -- Mark Myatt -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._