Hi, I just noticed the following bug: When we draw a random sample using the function stats::rnorm, there should be not auto-correlation in the sample. But their is some auto-correlation _when the sample that is drawn is small_. I describe the problem using two functions: DistributionAutocorrelation_Unexpected which as the wrong behavior : _when drawing some small samples using rnorm, there is generally a strong negative auto-correlation in the sample_. and DistributionAutocorrelation_Expected which illustrate the expected behavior *Unexpected : * DistributionAutocorrelation_Unexpected = function(SampleSize){ ? Cor = NULL ? for(repetition in 1:1e5){ ??? X = rnorm(SampleSize) ??? Cor[repetition] = cor(X[-1],X[-length(X)]) ? } ? return(Cor) } par(mfrow=c(3,3)) for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){ hist(DistributionAutocorrelation_Unexpected(SampleSize_),col='grey',main=paste0('SampleSize=',SampleSize_)) ; abline(v=0,col=2) } output: *Expected**:* DistributionAutocorrelation_Expected = function(SampleSize){ ? Cor = NULL ? for(repetition in 1:1e5){ ??? X = rnorm(SampleSize) *??? Cor[repetition] = cor(sample(X[-1]),sample(X[-length(X)]))* ? } ? return(Cor) } par(mfrow=c(3,3)) for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){ hist(DistributionAutocorrelation_Expected(SampleSize_),col='grey',main=paste0('SampleSize=',SampleSize_)) ; abline(v=0,col=2) } Some more information you might need: packageDescription("stats") Package: stats Version: 3.5.1 Priority: base Title: The R Stats Package Author: R Core Team and contributors worldwide Maintainer: R Core Team <R-core at r-project.org> Description: R statistical functions. License: Part of R 3.5.1 Imports: utils, grDevices, graphics Suggests: MASS, Matrix, SuppDists, methods, stats4 NeedsCompilation: yes Built: R 3.5.1; x86_64-pc-linux-gnu; 2018-07-03 02:12:37 UTC; unix Thanks for correcting that. fill free to ask any further information you would need. cheers, hugo -- - no title specified Hugo Math?-Hubert ATER Laboratoire Interdisciplinaire des Environnements Continentaux (LIEC) UMR 7360 CNRS - ?B?t IBISE Universit? de Lorraine ?- ?UFR SciFA 8, Rue du G?n?ral Delestraint F-57070 METZ +33(0)9 77 21 66 66 - - - - - - - - - - - - - - - - - - Les r?flexions naissent dans les doutes et meurent dans les certitudes. Les doutes sont donc un signe de force et les certitudes un signe de faiblesse. La plupart des gens sont pourtant certains du contraire. - - - - - - - - - - - - - - - - - - Thoughts appear from doubts and die in convictions. Therefore, doubts are an indication of strength and convictions an indication of weakness. Yet, most people believe the opposite.
Annaert Jan
2018-Oct-04 11:23 UTC
[R] Bug : Autocorrelation in sample drawn from stats::rnorm
Did you take into account that the sample serial correlation coefficient has a bias of approximately -1/T (with T the sample size)? Its variance is approximately 1/T. Jan Annaert -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of hmh Sent: donderdag 4 oktober 2018 12:09 To: R <r-help at r-project.org> Subject: [R] Bug : Autocorrelation in sample drawn from stats::rnorm Hi, I just noticed the following bug: When we draw a random sample using the function stats::rnorm, there should be not auto-correlation in the sample. But their is some auto-correlation _when the sample that is drawn is small_. I describe the problem using two functions: DistributionAutocorrelation_Unexpected which as the wrong behavior : _when drawing some small samples using rnorm, there is generally a strong negative auto-correlation in the sample_. and DistributionAutocorrelation_Expected which illustrate the expected behavior *Unexpected : * DistributionAutocorrelation_Unexpected = function(SampleSize){ ? Cor = NULL ? for(repetition in 1:1e5){ ??? X = rnorm(SampleSize) ??? Cor[repetition] = cor(X[-1],X[-length(X)]) ? } ? return(Cor) } par(mfrow=c(3,3)) for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){ hist(DistributionAutocorrelation_Unexpected(SampleSize_),col='grey',main=paste0('SampleSize=',SampleSize_)) ; abline(v=0,col=2) } output: *Expected**:* DistributionAutocorrelation_Expected = function(SampleSize){ ? Cor = NULL ? for(repetition in 1:1e5){ ??? X = rnorm(SampleSize) *??? Cor[repetition] = cor(sample(X[-1]),sample(X[-length(X)]))* ? } ? return(Cor) } par(mfrow=c(3,3)) for(SampleSize_ in c(4,5,6,7,8,10,15,20,50)){ hist(DistributionAutocorrelation_Expected(SampleSize_),col='grey',main=paste0('SampleSize=',SampleSize_)) ; abline(v=0,col=2) } Some more information you might need: packageDescription("stats") Package: stats Version: 3.5.1 Priority: base Title: The R Stats Package Author: R Core Team and contributors worldwide Maintainer: R Core Team <R-core at r-project.org> Description: R statistical functions. License: Part of R 3.5.1 Imports: utils, grDevices, graphics Suggests: MASS, Matrix, SuppDists, methods, stats4 NeedsCompilation: yes Built: R 3.5.1; x86_64-pc-linux-gnu; 2018-07-03 02:12:37 UTC; unix Thanks for correcting that. fill free to ask any further information you would need. cheers, hugo -- - no title specified Hugo Math?-Hubert ATER Laboratoire Interdisciplinaire des Environnements Continentaux (LIEC) UMR 7360 CNRS - ?B?t IBISE Universit? de Lorraine ?- ?UFR SciFA 8, Rue du G?n?ral Delestraint F-57070 METZ +33(0)9 77 21 66 66 - - - - - - - - - - - - - - - - - - Les r?flexions naissent dans les doutes et meurent dans les certitudes. Les doutes sont donc un signe de force et les certitudes un signe de faiblesse. La plupart des gens sont pourtant certains du contraire. - - - - - - - - - - - - - - - - - - Thoughts appear from doubts and die in convictions. Therefore, doubts are an indication of strength and convictions an indication of weakness. Yet, most people believe the opposite. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.