How to extract fitted values of GAM {mgcv} for each variable in R? -
i'm searching method add predicted (real, not standardized) values of every single variable in model
> model<-gam(ln_brutto~s(agecont,by=sex)+factor(sex)+te(month,age)+s(month,by=sex), data=bears)
this summary of model:
> summary(m13) family: gaussian link function: identity formula: ln_brutto ~ s(agecont, = sex) + factor(sex) + te(month, age) + s(month, = sex) parametric coefficients: estimate std. error t value pr(>|t|) (intercept) 4.32057 0.01071 403.34 <2e-16 *** factor(sex)m 0.27708 0.01376 20.14 <2e-16 *** --- signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 approximate significance of smooth terms: edf ref.df f p-value s(agecont):sexf 8.1611 8.7526 20.170 < 2e-16 *** s(agecont):sexm 6.6695 7.5523 32.689 < 2e-16 *** te(month,age) 10.3651 12.7201 6.784 2.19e-12 *** s(month):sexf 0.9701 0.9701 0.641 0.430 s(month):sexm 1.3750 1.6855 0.193 0.787 --- signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 rank: 60/62 r-sq.(adj) = 0.781 deviance explained = 78.7% gcv = 0.048221 scale est. = 0.046918 n = 1093
predicted values provided code:
> predict<-predict(m13, type = "terms")
and result looks this:
factor(sex) s(agecont):sexf s(agecont):sexm te(month,age) s(month):sexf s(month):sexm 1 0.2770806 0.000000000 0.111763696 -0.077845764 0.000000000 0.0007840912 2 0.2770806 0.000000000 0.240016156 -0.049143798 0.000000000 0.0007840912 3 0.2770806 0.000000000 0.034328752 0.046524454 0.000000000 -0.0058871897 4 0.0000000 -0.786533918 0.000000000 -0.067942427 0.021990192 0.0000000000 5 0.0000000 0.074434715 0.000000000 0.046524454 0.021990192 0.0000000000 6 0.0000000 0.161121563 0.000000000 0.089599601 0.021990192 0.0000000000 7 0.0000000 0.074434715 0.000000000 0.046524454 0.021990192 0.0000000000 8 0.2770806 0.000000000 -0.298597370 -0.007877328 0.000000000 -0.0058871897 ...
but guess these standardized predicted values , not real values (the real ones should have no negative values!?).
so know have modify in code, real values? idea? thank you!
not quite sure if follow correctly, predict(model, type = "terms")
might solution you're looking for.
update
i don't think these standardised. possibly of coefficients negative.
consider example file ?mgcv:::predict.gam
:
library(mgcv) n<-200 sig <- 2 dat <- gamsim(1,n=n,scale=sig) b<-gam(y~s(x0)+s(i(x1^2))+s(x2)+offset(x3),data=dat)
the results below illustrate these in fact contributions being used each predictor calculate fitted values (by calculating sum of each of these contributions , adding intercept , offset).
> head(predict(b)) 1 2 3 4 5 6 9.263322 2.822200 7.137201 4.902631 14.558401 11.889092 > head(rowsums(predict(b, type = "terms")) + attr(predict(b, type = "terms"), "constant") + dat$x3) 1 2 3 4 5 6 9.263322 2.822200 7.137201 4.902631 14.558401 11.889092
Comments
Post a Comment