gam

mgcv gam() error: model has more coefficients than data

末鹿安然 提交于 2020-01-01 07:13:43
问题 I am using GAM (generalized additive models) for my dataset. This dataset has 32 observations, with 6 predictor variables and a response variable (namely power). I am using gam() function of the mgcv package to fit the models. Whenever, I try to fit a model I do get an error message as: Error in gam(formula.hh, data = data, na.action = na.exclude, : Model has more coefficients than data From this error message, I infer that I have more predictor variables as compared to the number of

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

Could not find function plot.gam

柔情痞子 提交于 2019-12-24 02:25:28
问题 I'm going through the "Introduction to Statistical Learning with Applications in R" (ISLR), and I'm stuck on a part on page 295, the lab on Generalized Additive Models. When I run the following code I get an error Error in plot.gam(gam1, se = TRUE, col = "red") : could not find function "plot.gam" . library(ISLR) gam1 = lm(wage ~ ns(year, 4) + ns(age, 5) + education, data=Wage) par(mfrow=c(1,3)) plot.gam(gam1, se=TRUE, col="red") The book says that plot.gam should be part of the general plot

Difference between two geom_smooth() lines

给你一囗甜甜゛ 提交于 2019-12-23 00:52:25
问题 I made a plot for my data and am now I would like to have the difference in y for every x that was estimated by geom_smooth(). There is a similiar question which unfortunately has no answer. For example, how to get the differences for the following plot (data below): EDIT Two suggestions were made but I still don't know how to calculate the differences. First suggestion was to access the data from the ggplot object. I did so with pb <- ggplot_build(p) pb[["data"]][[1]] That approach kind of

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

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