Hi all, I am trying to teach myself to use and program R (How else do you do it? lol) Anyway, I wrote a piece of code to compute coefficent alpha for a scale. As I am neither a statistician nor a programmer, I wanted people's feedback. The code appears to work (Win 95 with R 1.0.1) and I have verified my result with SPSS and it was correct (much to my astonishment!). Nonetheless, I am sure there are better ways to do things than I did it as I have limited documentation and was reading as I programmed. Much of the process was trial and error for me (oops, error!, how can I fix that?). So, as an opportunity to learn, I thought I would send the code to the list for comments. Would be interested in hearing if there are potential problems with my code or easier ways of doing things. Please be nice, I know much of the "programming" here is probably naive. Best, Brett 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
There is a simpler way to compute alpha (in fact two other ways) in http://www.psych.upenn.edu/~baron/rpsych.htm and also in ..../rpsych.pdf (for those who prefer pdf). Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~jbaron -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 17 Jul 2000, Magill, Brett wrote:>I am trying to teach myself to use and program R (How else do you do it? >lol) Anyway, I wrote a piece of code to compute coefficent alpha for a >scale. As I am neither a statistician nor a programmer, I wanted people's >feedback.Neither am I, and pretty much a newbie too, just trying to do my best to help whenever I feel that I may have something. Which isn't often, but it happens. :-)>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)Well, I wouldn't define all those variables. Some you may need, some you don't. If you use it just once, then, you might just drop it.> for (i in 1:n.items) score.total<-score.total + x[,i]Let's see.... It's not clear to me what is happening, I can't get this to work, except in the case where you have a 1 * i matrix, I guess it is possible that you have that. Anyway, you seem to be summing _all_ the elements in x, in that case score.total<-sum(x) should do the job. If you would do a sum over just each of the indices, something like score.total<-apply(x, 1, sum) could do.> 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])Similar for them. Perhaps var.items<-apply(x, 1, function(y) sum(var(y))> var.total<-var(score.total) > z.var.total<-var(z.score.total)Eh, then, score.total wasn't a simple sum.... :-) But then it is perhaps the second suggestion up there.> 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)}This seems allright. Best, Kjetil -- Kjetil Kjernsmo Graduate astronomy-student Problems worthy of attack University of Oslo, Norway Prove their worth by hitting back E-mail: kjetikj at astro.uio.no - Piet Hein Homepage <URL:http://www.astro.uio.no/~kjetikj/> Webmaster at skepsis.no -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._