[R] Error in UseMethod("predict")
varin sacha
v@r|n@@ch@ @end|ng |rom y@hoo@|r
Sun Jan 17 11:19:27 CET 2021
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))
####################################
More information about the R-help
mailing list