mgcv

mgcv: Extract Knot Locations for `tp` smooth from a GAM model

本秂侑毒 提交于 2019-12-31 03:06:28
问题 I am trying to extract the placement of the knots from a GAM model in order to delineate my predictor variable into categories for another model. My data contains a binary response variable (used) and a continuous predictor (open). data <- data.frame(Used = rep(c(1,0,0,0),1250), Open = round(runif(5000,0,50), 0)) I fit the GAM as such: mod <- gam(Used ~ s(Open), binomial, data = data) I can get the predicted values, and the model matrix etc with either type=c("response", "lpmatrix") within

mgcv: How to return estimated smoothing parameter?

有些话、适合烂在心里 提交于 2019-12-24 07:27:34
问题 Consider the simple GAM fit as below: library(mgcv) my.gam <- gam(y~s(x), data=mydata) Is there anyway to return the estimated smoothing parameter (lambda) so that I can save it? I know that lambda is given in output as 'GCV score', but I need a specific code for returning it. How can I set lambda to a desired value? 回答1: summary() does not return smoothing parameters. You have mixed up GCV score with smoothing parameter. Consult a local statistician if you don't understand those concepts, or

Why does using “mgcv::s” in “gam(y ~ mgcv::s…)” result in an error?

空扰寡人 提交于 2019-12-23 10:56:47
问题 I wanted to be clear and use the :: notation in the lines for fitting an mgcv::gam . I stumbled over one thing when using the notation within the model call for mgcv::s . The code with a reproducible example / error is shown below. The reason is probably because I am using this notation within the model formula, but I could not figure out why this does not work / is not allowed. This is probably something quite specific concerning syntax (probably not mgcv specific, I guess), but maybe

How to extract fitted values of GAM {mgcv} for each variable in R?

旧时模样 提交于 2019-12-21 03:03:50
问题 I'm searching for a method to add the predicted (real, not standardized) values of every single variable in my model > model<-gam(LN_Brutto~s(agecont,by=Sex)+factor(Sex)+te(Month,Age)+s(Month,by=Sex), data=bears) This is the summary of my model: > summary(m13) Family: gaussian Link function: identity Formula: LN_Brutto ~ s(agecont, by = Sex) + factor(Sex) + te(Month, Age) + s(Month, by = Sex) Parametric coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 4.32057 0.01071 403.34 <2e

R plot.gam Error “Error in 1:object$nsdf : argument of length 0”

回眸只為那壹抹淺笑 提交于 2019-12-19 19:52:46
问题 I am trying to plot a gam object in R, which I made with the gam package. I receive the same error reported in Error in 1:object$nsdf : argument of length 0 when using plot.gam. However, the solution found there, updating to the latest versions (I think), is not working for me. I am running R 3.3.1, gam 1.12, and mgcv 1.8.12 (mgcv is where the plot.gam function is from). Unfortunately, I cannot share the data I am working with. However, the following code -- pulled directly from the p.294 of

Changing the Y axis of default plot.gam graphs

家住魔仙堡 提交于 2019-12-19 04:50:44
问题 I have run a GAM in R using the mgcv package with the following form: shark.gamFINAL <- gam(ln.raw.CPUE...0.1 ~ Year + Month + s(Mean.Temp, bs = "cr") + s(Mean.Chl.a, bs = "cr") + s(Mean.Front.density, bs = "cr"), data=r, family=gaussian) After running this model and calculating the percentage deviance explained by each variable I would like to plot the effect of each variable against the response However when I use the plot.gam function in R my graphs come out with a y axis that is "s

mgcv: how to specify interaction between smooth and factor?

与世无争的帅哥 提交于 2019-12-18 17:03:29
问题 This question was migrated from Cross Validated because it can be answered on Stack Overflow. Migrated 2 years ago . In R, I would like to fit a gam model with categorical variables. I thought I could do it like with (cat is the categorical variable). lm(data = df, formula = y ~ x1*cat + x2 + x3); But I can't do things like : gam(data = df, formula = y ~ s(x1)*cat + s(x2) + x3) but the following works: gam(data = df, formula = y ~ cat + s(x1) + s(x2) + x3) How do I add a categorical variable

mgcv: How to set number and / or locations of knots for splines

 ̄綄美尐妖づ 提交于 2019-12-17 18:29:48
问题 I want to use function gam in mgcv packages: x <- seq(0,60, len =600) y <- seq(0,1, len=600) prova <- gam(y ~ s(x, bs='cr') can I set the number of knots in s() ? and then can I know where are the knots that the spline used? Thanks! 回答1: It is always disappointing to see a wrong answer... While setting k is the correct way to go, fx = TRUE is definitely not right: it will force using pure regression spline without penalization. locations of knots For penalized regression spline, the exact

Error when trying to evaluate Markov Random Fields using mgcv::gam “mismatch between nb/polys supplied area names and data area names”

混江龙づ霸主 提交于 2019-12-13 14:42:07
问题 I tried to implement this great blog post by Gavin Simpson using data downloaded using the cancensus package, but I get the following error when trying to evaluate the gam: Error in smooth.construct.mrf.smooth.spec(object, dk$data, dk$knots) : mismatch between nb/polys supplied area names and data area names In addition: Warning message: In if (all.equal(sort(a.name), sort(levels(k))) != TRUE) stop("mismatch between nb/polys supplied area names and data area names") : the condition has length

xlim not working in plot.gam in mgcv

南笙酒味 提交于 2019-12-13 04:42:09
问题 The mgcv package in R lets you estimate smooth regressions on multiple variables. Here is an example: library(mgcv) set.seed(2) ## simulate some data... dat = gamSim(1,n=400,dist="normal",scale=2) mod = gam(y~te(x0, x1),data=dat) par(mfrow = c(2,1)) plot(mod) plot(mod, xlim = c(0, 0.5)) When I run this, the two plots look exactly the same. Why doesn't xlim = c(0, 0.5) restrict the range of the horizontal axis? How can I achieve this? Update on my session info: > sessionInfo() R version 3.0.1