[R] help in building a function
Uwe Ligges
ligges at statistik.uni-dortmund.de
Sat Sep 11 17:18:09 CEST 2004
Zhen Pang wrote:
>
>
>
>> From: assuncao.senra at portugalmail.com
>> To: R help <r-help at stat.math.ethz.ch>
>> Subject: [R] help in building a function
>> Date: Sat, 11 Sep 2004 14:22:13 +0100
>>
>> 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){
>
>
> note i+1:16 stands for 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))
>
>
> t is useless here, also you did not use t at other place
>
>>
>> r3<-sum(u*ml)*(sum(v*mc)/n
^ ^ ^ ^ ^ = 5, but 5%%2 ....
The syntax error is above: Please count the parentheses!
Please try to debug yourself. R has the tools to make it pretty easy!
As a first step, execute your function line by line, for example ... (as
you always should do during development).
Uwe Ligges
>> 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
>>
>>
>>
>> __________________________________________________________
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list