[R] Simulating correlations with varying sample sizes
Dennis Murphy
djmuser at gmail.com
Mon May 16 19:14:02 CEST 2011
Hi:
A more 'R-ish' way to do this would be to use sapply() in place of the
loop as follows:
N = c(1000,100,80,250,125,375,90,211,160,540)
corrfun <- function(n) {
require(MASS)
dat <- mvrnorm(n=n, mu=c(0,0), Sigma=matrix(c(1,.3,.3,1), 2))
r <- cor(dat)[1, 2]
}
sapply(N, corrfun)
# My result:
> sapply(N, corrfun)
[1] 0.29095517 0.42442863 0.04917587 0.28322478 0.42035862 0.30463885
[7] 0.28411963 0.35413437 0.32901814 0.26318604
In this case, sapply() applies corrfun() to each element of the vector
N and outputs a numeric vector, so there is no need to initialize a
vector in advance of a loop. The timings are about the same (0.45 sec
for 100 iterations of sapply() vs. 0.47 sec for 100 iterations of the
corrected loop), but the code is clearer (at least to R-helpers :) and
more compact.
HTH,
Dennis
On Mon, May 16, 2011 at 6:42 AM, Holger Steinmetz
<Holger.steinmetz at web.de> wrote:
> Hi there,
>
> I would like to draw 10 correlations from a bivariate population - but every
> draw should be done with a different sample size. I thought I could to this
> with a loop:
>
> r=numeric(10) #Goal vector
> N = c(1000,100,80,250,125,375,90,211,160,540) #Sample size vector
> for(i in 1:10) {
> data <- mvrnorm(n=N,mu=c(0,0),Sigma=matrix(c(1,.3,.3,1),2))
> r[i] <- cor(data[,1],data[,2])
> }
>
> Goal: The 10 correlations shall be contained in the r-vector.
> However, this does not work. I get an error that "arguments do not match"
>
> Has anybody an idea?
>
> Best,
> Holger
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Simulating-correlations-with-varying-sample-sizes-tp3526231p3526231.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.
>
More information about the R-help
mailing list