[R] replicate and as.matrix: different behaviour between batch and non-batch mode
Charles C. Berry
cberry at tajo.ucsd.edu
Thu Nov 6 17:17:25 CET 2008
On Thu, 6 Nov 2008, Oliver Bandel wrote:
> Hello,
>
>
> for a simulation I tried the following:
>
> =================================================================
> sampmeanvec <- function (from, n, repititions)
> {
> print( paste("samplesize n:", n, "repititions:", repititions) )
> samples.mat <- as.matrix( replicate( repititions, sample(from, n) ) )
>
> # would that case-check be necessary?
> #if( n == 1 )
> #{
> # samples.mat <- t (samples.mat)
> #}
>
> print( "Dim of matrix:")
> print( dim(samples.mat) )
>
> meanvec <- apply(samples.mat, 2, mean)
> return(meanvec)
> }
>
>
> gleichsamp <- runif(10000)
>
> for( sampsize in c(1,2,4,8,16,32,64,128) )
> {
> sampmeanvec(gleichsamp, sampsize, 20)
> }
>
> =================================================================
>
> The following result:
> ----------------------
>> source("central_limit_theorem.R")
> [1] "samplesize n: 1 repititions: 20"
> [1] "Dim of matrix:"
> [1] 20 1
> [1] "samplesize n: 2 repititions: 20"
> [1] "Dim of matrix:"
> [1] 2 20
> [1] "samplesize n: 4 repititions: 20"
> [1] "Dim of matrix:"
> [1] 4 20
> [1] "samplesize n: 8 repititions: 20"
> [1] "Dim of matrix:"
> [1] 8 20
> [1] "samplesize n: 16 repititions: 20"
> [1] "Dim of matrix:"
> [1] 16 20
> [1] "samplesize n: 32 repititions: 20"
> [1] "Dim of matrix:"
> [1] 32 20
> [1] "samplesize n: 64 repititions: 20"
> [1] "Dim of matrix:"
> [1] 64 20
> [1] "samplesize n: 128 repititions: 20"
> [1] "Dim of matrix:"
> [1] 128 20
>>
>
> Look at the first dimension: there the cols and rows are
> changed.
>
> I tried directly in the R-shell:
>
>
>> x <- 1:20
>> dim( as.matrix( replicate(1, sample(x, length(x)) ) ))
> [1] 20 1
>> dim( as.matrix( replicate(2, sample(x, length(x)) ) ))
> [1] 20 2
>> dim( as.matrix( replicate(3, sample(x, length(x)) ) ))
> [1] 20 3
>> dim( as.matrix( replicate(4, sample(x, length(x)) ) ))
> [1] 20 4
>
>
> This looks good (and correct to me).
Look again .
It is not the same as what you have above.
If studying your code does not reveal your error, you might put a
browser() call in your function after the samples.mat <- ... line to
get a sense of what is happening. See
?browser
>
>
> Can you locate my problem here?
>
> Why is the cols and rows dimensions be changed?
>
> Without using as.matrix the result of replicte is just a vector.
>
Not!
> is.matrix( replicate(4, sample(x, length(x)) ) )
[1] TRUE
So I have to use it.
> But only in the script/batch-mode.
>
> Typed in directly (see above), it works as expected.
>
> Any ideas on this behaviour?
Get some rest. Then make a big pot of coffee. Stuff like this happens to
many of us. You need to slow down and work thru your code before
jumping to conclusions about odd behavior of well tested, well documented
functions.
HTH,
Chuck
>
> Ciao,
> Oliver
>
> ______________________________________________
> 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.
>
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
More information about the R-help
mailing list