En indlejret tekst med ukendt tegns?t er blevet fjernet... Navn: ikke tilg?ngelig Url: https://stat.ethz.ch/pipermail/r-help/attachments/20071209/877bf190/attachment.pl
for (i in...)
3 messages · Rina Oldager Miehs, Duncan Murdoch, William Revelle
On 09/12/2007 3:04 PM, Rina Oldager Miehs wrote:
Hey do anyone know why this error occurs??
for(i in 1:n_trait){
+ for( j in 1:n_trait){
+ rG[i,j] <- (G_o[i,j]/(sqrt(G_o[i,i]%*%G_o[j,j]))
+ rP[i,j] <- (P_o[i,j]/(sqrt(P_o[i,i]%*%P_o[j,j]))
Error: unexpected symbol in:
" rG[i,j] <- (G_o[i,j]/(sqrt(G_o[i,i]%*%G_o[j,j]))
rP"
Your parentheses don't match on the rG line. You have 3 lefts and 2 rights. Duncan Murdoch
rE[i,j] <- (R_o[i,j]/(sqrt(R_o[i,i]%*%R_o[j,j]))
+ } Error: unexpected '}' in: " rE[i,j] <- (R_o[i,j]/(sqrt(R_o[i,i]%*%R_o[j,j])) }"
h2[i] <- (G_o[i,i]/P_o[i,i]) }
Error: unexpected '}' in "}" If i make parentes around each of the commands this comes instead:
for(i in 1:n_trait){
+ for( j in 1:n_trait){
+ (rG[i,j] <- (G_o[i,j]/(sqrt(G_o[i,i]%*%G_o[j,j])))
+ (rP[i,j] <- (P_o[i,j]/(sqrt(P_o[i,i]%*%P_o[j,j])))
+ (rE[i,j] <- (R_o[i,j]/(sqrt(R_o[i,i]%*%R_o[j,j])))
+ }
Error: unexpected '}' in:
" (rE[i,j] <- (R_o[i,j]/(sqrt(R_o[i,i]%*%R_o[j,j])))
}"
h2[i] <- (G_o[i,i]/P_o[i,i]) }
Error: unexpected '}' in "}" I dont understand what i am wrigthing wrong because i have a for command rigth before this one and that works...
for (i in 1:n_trait){
+ for ( j in 1:n_trait){
+ R_o[i,j] = P_o[i,j]-G_o[i,j]
+ }
+ }
Is it because it is matrices??
Cincerely Rina
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
At 3:27 PM -0500 12/9/07, Duncan Murdoch wrote:
On 09/12/2007 3:04 PM, Rina Oldager Miehs wrote:
Hey do anyone know why this error occurs??
>> for(i in 1:n_trait){
+ for( j in 1:n_trait){
+ rG[i,j] <- (G_o[i,j]/(sqrt(G_o[i,i]%*%G_o[j,j]))
+ rP[i,j] <- (P_o[i,j]/(sqrt(P_o[i,i]%*%P_o[j,j]))
Error: unexpected symbol in:
> " rG[i,j] <- (G_o[i,j]/(sqrt(G_o[i,i]%*%G_o[j,j]))
rP"
Your parentheses don't match on the rG line. You have 3 lefts and 2 rights. Duncan Murdoch
Rina,
Alternatively, you could avoid the entire double loop by using
matrix operations. I show this and then reproduce it using the first
section of your code:
#how to use matrices to avoid looping
n_trait <- 5
X <- matrix( rnorm(100*n_trait),ncol= n_trait) #create a data matrix
G_o <- t(X) %*% X #create the G_o matrix as a matrix of cross products
G_o #show the cross products
dG <- diag(1/sqrt(diag(G_o))) #divide by the sqrt of the diagonal
rG <- dG %*% G_o %*% dG
rG #show the result
#######################
#abbreviated Rina code using loops
for(i in 1:n_trait){
for( j in 1:n_trait){
rG[i,j] <- G_o[i,j]/(sqrt(G_o[i,i]%*%G_o[j,j])) }}
rG #compare to above
Bill
William Revelle http://personality-project.org/revelle.html Professor http://personality-project.org/personality.html Department of Psychology http://www.wcas.northwestern.edu/psych/ Northwestern University http://www.northwestern.edu/ Use R for statistics: http://personality-project.org/r