"NaN" answer don't understand why

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

"NaN" answer don't understand why

R help mailing list-2
Dear R-experts,

Here below my reproducible example. No error message but I can not get a result. I get "NaN" as a result. I don't understand what is going on. Many thanks for your precious help, as usual.


 # # # # # # # # # # # # # # # # # # # # # # # # #
x<-c(499,491,500,517,438,495,501,525,516,494,500,453,479,481,505,465,477,520,520,480,477,416,502,503,497,513,492,469,504,482,502,498,463,504,495)
y<-c(499,496,424,537,480,484,503,575,540,436,486,506,496,481,508,425,501,519,546,507,452,498,471,495,499,522,509,474,502,534,504,466,527,485,525)

library(robustgam)
true.family <- poisson()

#Robust GAM
fit=robustgam(x,y,sp=0,family=true.family,smooth.basis='ps',K=3)

#OLS
fit1 <- lm(y~x)

#Huber-M
library(robustbase)
library(MASS)
fit2=rlm(y~x)

#GAM
library(mgcv)
fit3=gam(y~s(x))

# MSE of OLS linear model
mean(residuals(fit1)^2)

# MSE of Huber-M linear model
mean(residuals(fit2)^2)

# MSE of GAM
mean(residuals(fit3)^2)

# MSE of robust GAM
mean(residuals(fit)^2)
 # # # # # # # # # # # # # # # # # # # # # # # # #
 

______________________________________________
[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: "NaN" answer don't understand why

Bill Dunlap-2
fit <- robustgam::robustgam(...) produces a list, with no class attached,
so residuals(fit) invokes the default method for residuals(), which
essentially returns the 'residuals' component of 'fit'.  There is no such
component so it returns NULL, an object of length zero.  The mean of a
length-zero object is NaN.

It would make sense for mean(NULL) or sum(NULL) to give an error since they
are only meant to work on numbers.  However this would probably break some
existing code, since there are various functions that return NULL instead
of numeric(0).

-Bill

On Fri, Nov 13, 2020 at 2:05 PM varin sacha via R-help <[hidden email]>
wrote:

> Dear R-experts,
>
> Here below my reproducible example. No error message but I can not get a
> result. I get "NaN" as a result. I don't understand what is going on. Many
> thanks for your precious help, as usual.
>
>
>  # # # # # # # # # # # # # # # # # # # # # # # # #
>
> x<-c(499,491,500,517,438,495,501,525,516,494,500,453,479,481,505,465,477,520,520,480,477,416,502,503,497,513,492,469,504,482,502,498,463,504,495)
>
> y<-c(499,496,424,537,480,484,503,575,540,436,486,506,496,481,508,425,501,519,546,507,452,498,471,495,499,522,509,474,502,534,504,466,527,485,525)
>
> library(robustgam)
> true.family <- poisson()
>
> #Robust GAM
> fit=robustgam(x,y,sp=0,family=true.family,smooth.basis='ps',K=3)
>
> #OLS
> fit1 <- lm(y~x)
>
> #Huber-M
> library(robustbase)
> library(MASS)
> fit2=rlm(y~x)
>
> #GAM
> library(mgcv)
> fit3=gam(y~s(x))
>
> # MSE of OLS linear model
> mean(residuals(fit1)^2)
>
> # MSE of Huber-M linear model
> mean(residuals(fit2)^2)
>
> # MSE of GAM
> mean(residuals(fit3)^2)
>
> # MSE of robust GAM
> mean(residuals(fit)^2)
>  # # # # # # # # # # # # # # # # # # # # # # # # #
>
>
> ______________________________________________
> [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: "NaN" answer don't understand why

R help mailing list-2
Dear Bill,

Many thanks for your response. I got it.

Best,







Le samedi 14 novembre 2020 à 00:17:10 UTC+1, Bill Dunlap <[hidden email]> a écrit :





fit <- robustgam::robustgam(...) produces a list, with no class attached, so residuals(fit) invokes the default method for residuals(), which essentially returns the 'residuals' component of 'fit'.  There is no such component so it returns NULL, an object of length zero.  The mean of a length-zero object is NaN.

It would make sense for mean(NULL) or sum(NULL) to give an error since they are only meant to work on numbers.  However this would probably break some existing code, since there are various functions that return NULL instead of numeric(0).

-Bill

On Fri, Nov 13, 2020 at 2:05 PM varin sacha via R-help <[hidden email]> wrote:

> Dear R-experts,
>
> Here below my reproducible example. No error message but I can not get a result. I get "NaN" as a result. I don't understand what is going on. Many thanks for your precious help, as usual.
>
>
>  # # # # # # # # # # # # # # # # # # # # # # # # #
> x<-c(499,491,500,517,438,495,501,525,516,494,500,453,479,481,505,465,477,520,520,480,477,416,502,503,497,513,492,469,504,482,502,498,463,504,495)
> y<-c(499,496,424,537,480,484,503,575,540,436,486,506,496,481,508,425,501,519,546,507,452,498,471,495,499,522,509,474,502,534,504,466,527,485,525)
>
> library(robustgam)
> true.family <- poisson()
>
> #Robust GAM
> fit=robustgam(x,y,sp=0,family=true.family,smooth.basis='ps',K=3)
>
> #OLS
> fit1 <- lm(y~x)
>
> #Huber-M
> library(robustbase)
> library(MASS)
> fit2=rlm(y~x)
>
> #GAM
> library(mgcv)
> fit3=gam(y~s(x))
>
> # MSE of OLS linear model
> mean(residuals(fit1)^2)
>
> # MSE of Huber-M linear model
> mean(residuals(fit2)^2)
>
> # MSE of GAM
> mean(residuals(fit3)^2)
>
> # MSE of robust GAM
> mean(residuals(fit)^2)
>  # # # # # # # # # # # # # # # # # # # # # # # # #
>  
>
> ______________________________________________
> [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.