[R] the function doesn´t work
Joshua Wiley
jwiley.psych at gmail.com
Sun Sep 26 23:46:56 CEST 2010
Hi,
You are correct that you do not know what the correlations are from
exactly. I tried to make up some examples to show you why.
######################################
#### Your Code ####
N = 10
n = 100
m = 5
k = n/m
l = matrix(0,nrow=m,ncol=N)
for(i in 1:N){
for(j in 1:m){
x=rnorm(n,0,0.5)
y=rnorm(n,0,0.8)
## inside for loops you need the print() command
## explicity if you want to see it
## Warning that this will create a lot of output
print(l)
l[j,i] = cor(
(x[(((j-1)*k)+1):(((j-1)*k)+k)]),
(y[(((j-1)*k)+1):(((j-1)*k)+k)]))
}
}
# Let's break this down a bit to make it more understandable
# First we do all the setup, straight forward enough
N = 10
n = 100
m = 5
k = n/m
l = matrix(0, nrow=m, ncol=N)
# Now the for loops
# Ignoring the outer one (for simplicity) you have
for(j in 1:m){
## rnorm() is called m times for both x and y
## which means every pass through the loop,
## their values will be different
x=rnorm(n,0,0.5)
y=rnorm(n,0,0.8)
## This just prints the values of l so far
print(l)
## this assigns the results of the call to cor() to a particular
## cell in the matrix l
l[j,i] = cor(
## You are finding the correlation between a subset of
## x and y values (based on the current values of i and j
## BUT, x and y themselves are changing everytime
## so what you are actually doing is taking a random sample
## and then taking a non-random subsample from this
## which should just work out to be a smaller random sample
(x[(((j-1)*k)+1):(((j-1)*k)+k)]),
(y[(((j-1)*k)+1):(((j-1)*k)+k)])
)
}
# Another way to look at this that might make it a bit clearer
# Going back to your full code
N = 10
n = 100
m = 5
k = n/m
l = matrix(0,nrow=m,ncol=N)
## create another matrix with same dimensions as l
vals = matrix(0, nrow = m, ncol = N)
for(i in 1:N){
for(j in 1:m){
x=rnorm(n,0,0.5)
y=rnorm(n,0,0.8)
## create an object storing what values
## you are selecting (so we can see it later)
vals[j, i] = paste((((j-1)*k)+1), (((j-1)*k)+k), sep = ":")
l[j,i] = cor(
(x[(((j-1)*k)+1):(((j-1)*k)+k)]),
(y[(((j-1)*k)+1):(((j-1)*k)+k)]))
}
}
## now we can print the matrix of correlations
## and the the numbers used to subset them
## This lends some insight into the subsets being used
## but as I mentioned before, x and y are changing each time
vals
l
# One way to get around this, would be to assign x and y
# outside of your for loop
# For instance, lets say you wanted to regenerate x and y only
# for each column (rather than for every single correlation)
# This makes some sense given the subsets you select
# are unique within a column, but not across rows
N = 10
n = 100
m = 5
k = n/m
l = matrix(0,nrow=m,ncol=N)
## outer loop
for(i in 1:N){
## I have moved the assignment into the outer loop
## now x and y will only be regenerated N times
## rather than N * m times
x=rnorm(n,0,0.5)
y=rnorm(n,0,0.8)
## inner loop
for(j in 1:m){
l[j,i] = cor(
(x[(((j-1)*k)+1):(((j-1)*k)+k)]),
(y[(((j-1)*k)+1):(((j-1)*k)+k)]))
}
}
l # print matrix of correlations
##########################
HTH,
Josh
On Sun, Sep 26, 2010 at 1:50 PM, jethi <kartija at hotmail.com> wrote:
>
> thanks a lot for ur patience and understandig, josh. ok perhaps it would be
> help me to change my programm by step by step. the first important thing of
> my programm is to caluculate the correlation of each block of a radom
> variable.
>
> so i have a n bivariate random sample wich i saperate in m blocks and
> calculate the correlation. i designe a matrix for that. so that i watch not
> one n bivariate random sample, but N bivariate random sample. (its important
> to watch a matrix because for my powerfunction later)
>
> so i programm my correlationfunction like that
>
> N=10
> n=100
> m=5
> k=n/m
> l=matrix(0,nrow=m,ncol=N)
> for(i in 1:N){
>
> for(j in 1:m){
> x=rnorm(n,0,0.5)
> y=rnorm(n,0,0.8)
> l
> l[j,i]=cor((x[(((j-1)*k)+1):(((j-1)*k)+k)]),
> (y[(((j-1)*k)+1):(((j-1)*k)+k)]))
> }
> }
> i think their is a mistake, am i right? if i watch this matrix, i don´t
> exactliy know which correlations are to which random sample, do i? i hope u
> have still time to help me.
>
> regards
> jethi
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/the-function-doesn-t-work-tp2714105p2714702.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-help
mailing list