0

I want to fit a smoothing spline using gam function. An attempt to plot the fitted values is resulting in an error -object$nsdf? I am wondering if that is a needed input and if so what is df referring to? How to fix this code.

gam.fit=gam(y~s(disp,6)+s(hp,5)+s(wt,5), data=train.dat)
mean((test.dat$y - gam.pred)^2)  # 0.0002282536
plot(gam.fit, se=TRUE, col="blue",main="10.3f.gam")
# Error in 1:object$nsdf : argument of length 0

Thank you. Sincerely, Mary A. Marion

Mary A. Marion
  • 780
  • 1
  • 8
  • 27

1 Answers1

2

Your gam() call syntax:

gam.fit=gam(y~s(disp,6)+s(hp,5)+s(wt,5), data=train.dat)

suggests that you are using package gam instead of mgcv. However, the error you get, which complains about object$nsdf, is produced from mgcv package. Do not load both packages into your R session at the same time!!.

library(gam)
set.seed(0)
dat <- data.frame(x1 = rnorm(100), x2 = rnorm(100), x3 = rnorm(100),
                  y = rnorm(100))
fit <- gam(y ~ s(x1,6) + s(x2,5) + s(x3,5), data = dat)
par(mfrow = c(1,3)); plot.gam(fit)

enter image description here

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
  • thank you for your reply. I have not loaded mgcv unless it is coming in with something else but even then I have reduced the code to only the essentials and I stil get the same error. My installation of R is corrupted somehow. – Mary A. Marion Jul 19 '16 at 18:01
  • I have uninstalled R 3..3.0 and R 3.3.1. Having 2 versions on my system was getting things mixed up. Your code works beautifully now. Thanks – Mary A. Marion Jul 19 '16 at 19:13
  • My code is still not working. Is the data.frame statement critical here? – Mary A. Marion Jul 19 '16 at 19:52
  • Please see result of print(.packages() + ) [1] "tree" "pls" "MASS" "leaps" "ISLR" "Hmisc" [7] "ggplot2" "Formula" "survival" "lattice" "gam" "foreach" [13] "splines" "devtools" "car" "boot" "stats" "graphics" [19] "grDevices" "utils" "datasets" "methods" "base" > – Mary A. Marion Jul 21 '16 at 01:45
  • I think the gam program is expecting an x or x1 x2 x3 in its parameter list. That is what you have. Unfortunately I am still getting an error when I use that. – Mary A. Marion Jul 21 '16 at 01:46
  • I have tried to match your code. dat=as.data.frame(x1=s(disp,6),x2=s(hp,5),x3=s(wt,5),y=y) Error in as.data.frame(x1 = s(disp, 6), x2 = s(hp, 5), x3 = s(wt, 5), : argument "x" is missing, with no default – Mary A. Marion Jul 21 '16 at 02:51
  • I think I should look at the binary version of this package or at least source code. I have never done that but maybe that may help. – Mary A. Marion Jul 21 '16 at 08:00