# [R] Code for Coefficent (Cronbach's) Alpha

Kjetil Kjernsmo kjetil.kjernsmo at astro.uio.no
Mon Jul 17 22:55:39 CEST 2000

```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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```