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 <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.


______________________________________________
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.

Reply via email to