Skip to content

Get a result but an error message as well ?

9 messages · Richard M. Heiberger, varin sacha, Jeff Newmiller +1 more

#
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)
########################################
?
#
[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.
NULL
[1] 500
[1]   1 500


On Tue, May 19, 2020 at 3:51 PM varin sacha via R-help <r-help at r-project.org>
wrote:

  
  
#
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 ?







Le mardi 19 mai 2020 ? 21:59:07 UTC+2, Richard M. Heiberger <rmh at temple.edu> a ?crit :
[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.
NULL
[1] 500
[1] ? 1 500
On Tue, May 19, 2020 at 3:51 PM varin sacha via R-help <r-help at r-project.org> wrote:
#
Hello,

Inline.

?s 21:38 de 19/05/20, varin sacha via R-help escreveu:
Just don't transpose the output of replicate?

Hope this helps,

Rui Barradas
#
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:
Just don't transpose the output of replicate?

Hope this helps,

Rui Barradas
#
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:

            

  
  
#
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:

  
    
#
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:
7 days later
#
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: