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 at 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 at 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.
>
[[alternative HTML version deleted]]