Works for me. set.seed( 42 ) a <- c(2,4,3,4,6,5,3,1,2,3,4,3,4,5,65) b <- c(23,45,32,12,23,43,56,44,33,11,12,54,23,34,54) d <- c(9,4,5,3,2,1,3,4,5,6,4,9,10,11,18)
my.experiment <- function() { a <- a + rnorm( length( a ), 0, 0.05 ) b <- b + rnorm( length( b ), 0, 0.05 ) d <- d + rnorm( length( d ), 0, 0.05 ) OLS <- lm( a ~ b+d ) MSE_OLS <- mean( OLS$residuals^2 ) MSE_OLS } my.data <- replicate( 500 , my.experiment() ) mean( my.data ) On May 19, 2020 2:30:09 PM PDT, "Richard M. Heiberger" <r...@temple.edu> wrote: >you need to pay attention to the intermediate structures that you >generate. >If all you want is one number, then that is what you should create > >mean(replicate( 500, my.experiment() )) > >Since you do seem to want to store the intermediate values, then you >must >name >the object according to its structure. A one-dimensional vector takes >names(), not dimnames(). >A two-dimensional structure takes dimnames, and the number of row names >and >the number of column >names must match the number of rows and columns. > > >On Tue, May 19, 2020 at 5:25 PM varin sacha <varinsa...@yahoo.fr> >wrote: > >> Hi Rui, >> >> If I don't transpose t() the output of the replicate (my R code here >> below) I still get an error message !! >> >> ######################################## >> a=c(2,4,3,4,6,5,3,1,2,3,4,3,4,5,65) >> b=c(23,45,32,12,23,43,56,44,33,11,12,54,23,34,54) >> d=c(9,4,5,3,2,1,3,4,5,6,4,9,10,11,18) >> >> my.experiment <- function() { >> >> OLS <- lm( a ~ b+d ) >> >> MSE_OLS<-mean(OLS$residuals^2) >> >> return( c(MSE_OLS) ) >> } >> >> my.data = replicate( 500, my.experiment() ) >> colnames(my.data) <- c("MSE_OLS") >> mean(my.data) >> ######################################## >> >> >> >> >> >> >> Le mardi 19 mai 2020 à 23:14:21 UTC+2, Rui Barradas ><ruipbarra...@sapo.pt> >> a écrit : >> >> >> >> >> >> Hello, >> >> Inline. >> >> Às 21:38 de 19/05/20, varin sacha via R-help escreveu: >> > >> > Hi Richard, >> > >> > Thanks for your response. >> > However, how can I correct my R code knowing that I want, as a >result, >> only one value : the mean of the 500 MSE_OLS values ? >> >> Just don't transpose the output of replicate? >> >> Hope this helps, >> >> Rui Barradas >> >> > >> > >> > >> > >> > >> > >> > >> > Le mardi 19 mai 2020 à 21:59:07 UTC+2, Richard M. Heiberger < >> r...@temple.edu> a écrit : >> > >> > >> > >> > >> > >> >> dim(my.data) >> > [1] 1 500 >> > >> > you have a matrix with a single row and 500 columns. >> > you gave a name to only the first column. >> > >> > Look at the result of replicate(). it is a vector. You transposed >it >> into a one-row matrix. >> > >> >> tmp <- replicate( 500, my.experiment() ) >> >> dim(tmp) >> > NULL >> >> length(tmp) >> > [1] 500 >> >> dim(t(tmp)) >> > [1] 1 500 >> > >> > >> > On Tue, May 19, 2020 at 3:51 PM varin sacha via R-help < >> r-help@r-project.org> wrote: >> >> Dear R-experts, >> >> >> >> Here is my R code, I get a result but I also get an error message >so I >> doubt I can trust the result I get. >> >> What is going wrong ? Many thanks. >> >> >> >> ######################################## >> >> a<-c(2,4,3,4,6,5,3,1,2,3,4,3,4,5,65) >> >> b<-c(23,45,32,12,23,43,56,44,33,11,12,54,23,34,54) >> >> d<-c(9,4,5,3,2,1,3,4,5,6,4,9,10,11,18) >> >> >> >> my.experiment <- function( ) { >> >> >> >> OLS <- lm( a ~ b+d ) >> >> MSE_OLS<-mean(OLS$residuals^2) >> >> return( c(MSE_OLS) ) >> >> } >> >> >> >> my.data = t(replicate( 500, my.experiment() )) >> >> colnames(my.data) <- c("MSE_OLS") >> >> mean(my.data) >> >> ######################################## >> >> >> >> >> >> ______________________________________________ >> >> R-help@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. >> >> >> > >> > ______________________________________________ >> > R-help@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. >> > >> > > [[alternative HTML version deleted]] > >______________________________________________ >R-help@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. -- Sent from my phone. Please excuse my brevity. ______________________________________________ R-help@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.