[R] help in building a function

assuncao.senra@portugalmail.com assuncao.senra at portugalmail.com
Sat Sep 11 15:22:13 CEST 2004


hello,
i'm new in writting functions in R. I read the R-help manuals but still 
couldn't get it right.
The problem is: 
I have data on 16 categorical variables for 335 individuals. the data are 
lines for individuals and columns for variables.
I want to calculate the correlation between two variables using a formula 
given by Agresti for ordinal categorical variables.
I have to calcule the value for each pair of variables and save the result in 
a vector...
I wrote the following code

X<-as.matrix(X)

n=335
for (i in 1:15){
for (j in i+1:16){
Tab<-table(X[,i],X[,j])
u<-1:nrow(Tab)
v<-1:ncol(Tab)

ml<-margin.table(Tab,1)
mc<-margin.table(Tab,2)

r1<-u*Tab
r2<-sum(v*t(r1))

r3<-sum(u*ml)*(sum(v*mc)/n

r5<-sum((v^2)*mc)-((sum(v*mc))^2)/n

r4<-sum((u^2)*ml)-((sum(u*ml))^2)/n
r<-(r2-r3)/sqrt(r4*r5)

r
m=(n-1)*r^2
}
}
The correlation is r.I have a result, but there is a warning regarding r5 
about sintax.
Plus, it doesn´t perform the loops and I don't know how to save the results in 
a vector.
thanks for any help

Assunção



__________________________________________________________




More information about the R-help mailing list