[R] Error in UseMethod("predict")

varin sacha v@r|n@@ch@ @end|ng |rom y@hoo@|r
Sun Jan 17 14:22:26 CET 2021


Dear Eric,

Many thanks, I correct your 2 points and now I get another error message (Error in splineDesign(knots, x, ord, derivs, outer.ok = outer.ok, sparse = sparse) :
  empty 'derivs'). 
I have googleized and found some hints like (outer.ok=TRUE) but no one seems to work.

https://r.789695.n4.nabble.com/mgcv-gam-predict-problem-td3411006.html 

Any idea to make my code work would be appreciated.

Here below my new R code :

##########################
#Data
y=c(34000,45000,19000,48900,65000,67000,78000,90000,51000,32000,54000,85000,38000,76345,87654,90990,78654,67894,56789,65432,18998,78987,67543,45678,76543,67876)
x=c(345,543,543,456,432,378,543,579,432,254,346,564,611,543,542,632,345,468,476,487,453,356,490,499,567,532)

Dataset=data.frame(y,x)

#Plot
plot(x,y)

#Robust GAM
library(robustgam)
true.family <- poisson()
fit=robustgam(x,y, sp=2424,family=true.family,smooth.basis='ps',K=3)
x.new <- seq(range(x)[1], range(x)[2])
robustfit.new <- pred.robustgam(fit, data.frame(X=x.new))$predict.values
lines(x.new, robustfit.new, col="green", lwd=2)

# To find the « sp » to include in the fit function here above
robustfit.gic<-robustgam.GIC.optim(x,y,family=true.family,p=3,c=1.6,show.msg=FALSE,smooth.basis="ps", method="L-BFGS-B")

## CROSS VALIDATION REPLICATIONS MSE ROBUST GAM
install.packages("ISLR")
library(ISLR)

# Create a list to store the results
lst<-list()

# This statement does the repetitions (looping)
for(i in 1 :1000)
{

n=dim(Dataset)[1]
p=0.667
sam=sample(1 :n,floor(p*n),replace=FALSE)
Training =Dataset [sam,]
Testing = Dataset [-sam,] 

fit18<-robustgam(x,y, sp=4356,family=true.family,smooth.basis='ps',K=3)

ypred=pred.robustgam(fit18,data.frame(X=Testing))
MSE = mean((y-ypred)^2)
MSE
lst[i]<-MSE
}
mean(unlist(lst))
####################################



 Le dimanche 17 janvier 2021 à 11:41:49 UTC+1, Eric Berger <ericjberger using gmail.com> a écrit : 


Hi Sacha,
I never used these packages before but I installed them and tried your code. I have a few observations that may help.

1. the statement
    ypred = predict(fit18,newdata=Testing)
    is wrong. Checkout the help page (?robustgam)  which shows in the Examples section at the bottom to use something like
    ypred = pred.robustgam( fit18, data.frame(X=Testing)

2. your logic is wrong. You define the vectors x and y at the top. They should remain untouched during your program.
    However in the loop you redefine y and then use the redefined y as an argument to robustgam() the next time through
    the loop. This looks like a serious error.

HTH,
Eric


On Sun, Jan 17, 2021 at 12:20 PM varin sacha via R-help <r-help using r-project.org> wrote:
> Dear R-experts,
> 
> Here below my reproducible R code. I get an error message (end of code) I can't solve.
> Many thanks for your help.
> 
> ##########################
> #Data
> y=c(34000,45000,19000,48900,65000,67000,78000,90000,51000,32000,54000,85000,38000,76345,87654,90990,78654,67894,56789,65432,18998,78987,67543,45678,76543,67876)
> x=c(345,543,543,456,432,378,543,579,432,254,346,564,611,543,542,632,345,468,476,487,453,356,490,499,567,532)
> 
> Dataset=data.frame(y,x)
> 
> #Plot
> plot(x,y)
> 
> #Robust GAM
> library(robustgam)
> true.family <- poisson()
> fit=robustgam(x,y, sp=4356,family=true.family,smooth.basis='ps',K=3)
> x.new <- seq(range(x)[1], range(x)[2])
> robustfit.new <- pred.robustgam(fit, data.frame(X=x.new))$predict.values
> lines(x.new, robustfit.new, col="green", lwd=2)
> 
> # To find the « sp » to include in the fit function here above
> robustfit.gic<-robustgam.GIC.optim(x,y,family=true.family,p=3,c=1.6,show.msg=FALSE,smooth.basis="tp", method="L-BFGS-B")
> 
> ## CROSS VALIDATION REPLICATIONS MSE ROBUST GAM
> install.packages("ISLR")
> library(ISLR)
> 
> # Create a list to store the results
> lst<-list()
> 
> # This statement does the repetitions (looping)
> for(i in 1 :1000)
> {
> 
> n=dim(Dataset)[1]
> p=0.667
> sam=sample(1 :n,floor(p*n),replace=FALSE)
> Training =Dataset [sam,]
> Testing = Dataset [-sam,]
> 
> fit18<-robustgam(x,y, sp=4356,family=true.family,smooth.basis='ps',K=3)
> 
> ypred=predict(fit18,newdata=Testing)
> y=Dataset[-sam,]$y
> MSE = mean((y-ypred)^2)
> MSE
> lst[i]<-MSE
> }
> mean(unlist(lst))
> ####################################
>  
> 
> ______________________________________________
> R-help using 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.
> 



More information about the R-help mailing list