1
votes

I am attempting to run a GAM model and obtain a precise estimate, at a given value based on the recommendation from this thread for the package mgcv. When using the predict function, the results do not match the estimates from GAM.

Here's my model that I am interested in:

mod <- gam(y~s(a, by=b), data = dat)

When I obtain results using:

plot(mod)

The results are different than when I try to estimate a single plot point on variable a using the following method:

pdat <- with(dat,
             data.frame(a = 0,
                        b = mean(b,na.rm = TRUE)))
pred <- predict(mod, pdat, type = "response", se.fit = TRUE)
pdat <- transform(pdat, fitted = pred$fit)
pdat <- transform(pdat, upper = fitted + (1.96 * pred$se.fit),
                  lower = fitted - (1.96 * pred$se.fit))

Does anyone have any suggestions on what I may be doing wrong?

1

1 Answers

0
votes

Through trial and error I found the answer to my question. It seams, rather than the mean of the values because of my by variable I need to replace b with 1 -- this obtains the exact values in the graph.

Perhaps this will help someone else in the future.