varin sacha
2020-May-19 21:25 UTC
[R] [External] Get a result but an error message as well ?
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 <ruipbarradas at 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 <rmh at 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 at 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 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. >> > > ______________________________________________ > 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. >
Richard M. Heiberger
2020-May-19 21:30 UTC
[R] [External] Re: [External] Get a result but an error message as well ?
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 <varinsacha at 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 <ruipbarradas at 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 < > rmh at 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 at 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 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. > >> > > > > ______________________________________________ > > 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. > > >[[alternative HTML version deleted]]
Jeff Newmiller
2020-May-19 21:44 UTC
[R] [External] Re: [External] Get a result but an error message as well ?
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" <rmh at 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 <varinsacha at 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 ><ruipbarradas at 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 < >> rmh at 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 at 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 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. >> >> >> > >> > ______________________________________________ >> > 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. >> > >> > > [[alternative HTML version deleted]] > >______________________________________________ >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.-- Sent from my phone. Please excuse my brevity.
Rui Barradas
2020-May-20 07:09 UTC
[R] [External] Get a result but an error message as well ?
Hello, I get an error but when running colnames(), not mean(). Notes: 1. I have changed the way the residuals are extracted, if there is a function resid(), the recommended practice is to use it. 2. c("MSE_OLS") and "MSE_OLS" are the identical() objects. Likewise, to return( c(MSE_OLS) ) is to have only MSE_OLS as the last function line. I find the latter clearer. my.experiment <- function() { OLS <- lm(a ~ b + d) #MSE_OLS <- mean(OLS$residuals^2) MSE_OLS <- mean(resid(OLS)^2) MSE_OLS } my.data <- replicate( 500, my.experiment() ) class(my.data) # it's a vector, not a matrix #[1] "numeric" colnames(my.data) <- "MSE_OLS" #Error in `colnames<-`(`*tmp*`, value = "MSE_OLS") : # attempt to set 'colnames' on an object with less than two dimensions mean(my.data) #[1] 105.6951 Hope this helps, Rui Barradas ?s 22:25 de 19/05/20, varin sacha escreveu:> 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 <ruipbarradas at 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 <rmh at 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 at 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 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. >>> >> >> ______________________________________________ >> 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. >>
varin sacha
2020-May-27 17:59 UTC
[R] [External] Get a result but an error message as well ?
Hi Rui, Richard and other, Many thanks for your responses that solve my problem. Best, SV Le mercredi 20 mai 2020 ? 09:09:49 UTC+2, Rui Barradas <ruipbarradas at sapo.pt> a ?crit : Hello, I get an error but when running colnames(), not mean(). Notes: 1. I have changed the way the residuals are extracted, if there is a function resid(), the recommended practice is to use it. 2. c("MSE_OLS") and "MSE_OLS" are the identical() objects. Likewise, to return( c(MSE_OLS) ) is to have only MSE_OLS as the last function line. I find the latter clearer. my.experiment <- function() { ? OLS <- lm(a ~ b + d) ? #MSE_OLS <- mean(OLS$residuals^2) ? MSE_OLS <- mean(resid(OLS)^2) ? MSE_OLS } my.data <- replicate( 500, my.experiment() ) class(my.data)? # it's a vector, not a matrix #[1] "numeric" colnames(my.data) <- "MSE_OLS" #Error in `colnames<-`(`*tmp*`, value = "MSE_OLS") : #? attempt to set 'colnames' on an object with less than two dimensions mean(my.data) #[1] 105.6951 Hope this helps, Rui Barradas ?s 22:25 de 19/05/20, varin sacha escreveu:> 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 <ruipbarradas at 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 <rmh at 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 at 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 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. >>> >> >> ______________________________________________ >> 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. >>