Get a result but an error message as well ?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Get a result but an error message as well ?

R help mailing list-2
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)
########################################
 

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Get a result but an error message as well ?

Richard M. Heiberger
> 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 <[hidden email]>
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)
> ########################################
>
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Get a result but an error message as well ?

R help mailing list-2

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 <[hidden email]> 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 <[hidden email]> 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)
> ########################################
>  
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Get a result but an error message as well ?

Rui Barradas
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 <[hidden email]> 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 <[hidden email]> 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)
>> ########################################
>>  
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Get a result but an error message as well ?

R help mailing list-2
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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)
>> ########################################
>> 
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Re: [External] Get a result but an error message as well ?

Richard M. Heiberger
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 <[hidden email]> 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 <[hidden email]>
> 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 <
> [hidden email]> 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 <
> [hidden email]> 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)
> >> ########################################
> >>
> >>
> >> ______________________________________________
> >> [hidden email] 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.
> >>
> >
> > ______________________________________________
> > [hidden email] 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]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Re: [External] Get a result but an error message as well ?

Jeff Newmiller
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" <[hidden email]> 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 <[hidden email]>
>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
><[hidden email]>
>> 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 <
>> [hidden email]> 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 <
>> [hidden email]> 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)
>> >> ########################################
>> >>
>> >>
>> >> ______________________________________________
>> >> [hidden email] 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.
>> >>
>> >
>> > ______________________________________________
>> > [hidden email] 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]]
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Get a result but an error message as well ?

Rui Barradas
In reply to this post by R help mailing list-2
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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)
>>> ########################################
>>>    
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>> ______________________________________________
>> [hidden email] 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.
>>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: [External] Get a result but an error message as well ?

R help mailing list-2
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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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)
>>> ########################################
>>>   
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>>
>>
>> ______________________________________________
>> [hidden email] 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.
>>

______________________________________________
[hidden email] 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.