mgcv

mgcv: how to specify interaction between smooth and factor?

天涯浪子 提交于 2019-11-30 15:15:26
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 to just one of the splines? One of the comments has more or less told you how. Use by variable: s(x1, by = cat) This creates the "factor smooth" smoothing class fs , where a smooth function of x1 is created for each

R Package conflict between gam and mgcv?

♀尐吖头ヾ 提交于 2019-11-30 04:59:47
问题 Detaching packages in R isnt good practice (see ?detach ), but for some reasons I have to switch between the packages gam and mgcv . Once mgcv was attached and detached (and all the dependencies in the namespace unloaded!), functions of gam produce some strange errors (please forgive the terminology). It seems that - even though unloaded one step before - mgcv and friends are back in the namespace and function dispatching goes wrong. Does anyone had the same problem before? # fresh session t

Is it possible to plot the smooth components of a gam fit with ggplot2?

◇◆丶佛笑我妖孽 提交于 2019-11-29 20:12:11
I am fitting a model using gam from the mgcv package and store the result in model and so far I have been looking at the smooth components using plot(model) . I have recently started using ggplot2 and like its output. So I am wondering, is it possible to plot these graphs using ggplot2? Here is an example: x1 = rnorm(1000) x2 = rnorm(1000) n = rpois(1000, exp(x1) + x2^2) model = gam(n ~ s(x1, k=10) + s(x2, k=20), family="poisson") plot(model, rug=FALSE, select=1) plot(model, rug=FALSE, select=2) And I am interest in s(x1, k=10) and s(x2, k=20) not in the fit. Partial answer: I dug deeper into

installation path not writable R, unable to update packages

半腔热情 提交于 2019-11-29 11:54:53
问题 I am trying to install Bioconductor into R, using the code on their website. When I type in the code (see bellow) I get an error message saying that some packages can't be updated, the installation path is unwriteable. > ## try http:// if https:// URLs are not supported > source("https://bioconductor.org/biocLite.R") Bioconductor version 3.4 (BiocInstaller 1.24.0), ?biocLite for help > biocLite() BioC_mirror: https://bioconductor.org Using Bioconductor 3.4 (BiocInstaller 1.24.0), R 3.3.2

mgcv: how to extract knots, basis, coefficients and predictions for P-splines in adaptive smooth?

£可爱£侵袭症+ 提交于 2019-11-29 00:15:19
I'm using the mgcv package in R to fit some polynomial splines to some data via: x.gam <- gam(cts ~ s(time, bs = "ad"), data = x.dd, family = poisson(link = "log")) I'm trying to extract the functional form of the fit. x.gam is a gamObject , and I've been reading the documentation but haven't found enough information in order to manually reconstruct the fitted function. x.gam$smooth contains information about whether the knots have been placed; x.gam$coefficients gives the spline coefficients, but I don't know what order polynomial splines are used and looking in the code has not revealed

Is it possible to plot the smooth components of a gam fit with ggplot2?

筅森魡賤 提交于 2019-11-28 16:01:50
问题 I am fitting a model using gam from the mgcv package and store the result in model and so far I have been looking at the smooth components using plot(model) . I have recently started using ggplot2 and like its output. So I am wondering, is it possible to plot these graphs using ggplot2? Here is an example: x1 = rnorm(1000) x2 = rnorm(1000) n = rpois(1000, exp(x1) + x2^2) model = gam(n ~ s(x1, k=10) + s(x2, k=20), family="poisson") plot(model, rug=FALSE, select=1) plot(model, rug=FALSE, select

Extracting data used to make a smooth plot in mgcv

泄露秘密 提交于 2019-11-28 09:56:12
This thread from a couple of years ago describes how to extract data used to plot the smooth components of a fitted gam model. It works, but only when there is one smooth variable. I've got more than one smooth variable, and unfortunately I can only extract the smooths from the last of the series. Here is an example: library(mgcv) a = rnorm(100) b = runif(100) y = a*b/(a+b) mod = gam(y~s(a)+s(b)) summary(mod) plotData <- list() trace(mgcv:::plot.gam, at=list(c(25,3,3,3)), #this gets you to the location where plot.gam calls plot.mgcv.smooth (see ?trace) #plot.mgcv.smooth is the function that

How to extract fitted splines from a GAM (`mgcv::gam`)

耗尽温柔 提交于 2019-11-28 03:52:12
I am using GAM to model time trends in a logistic regression. Yet I would like to extract the the fitted spline from it to add it to another model, that cannot be fitted in GAM or GAMM. Thus I have 2 questions: How can I fit a smoother over time so that I force one knot to be at a particular location while letting the model to find the other knots? How can I extract the matrix from the fitted GAM so that I can use it in as an impute for a different model? The types of models I am running are to the following form: gam <- gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+ s(birth_year

mgcv: how to extract knots, basis, coefficients and predictions for P-splines in adaptive smooth?

青春壹個敷衍的年華 提交于 2019-11-27 14:09:13
问题 I'm using the mgcv package in R to fit some polynomial splines to some data via: x.gam <- gam(cts ~ s(time, bs = "ad"), data = x.dd, family = poisson(link = "log")) I'm trying to extract the functional form of the fit. x.gam is a gamObject , and I've been reading the documentation but haven't found enough information in order to manually reconstruct the fitted function. x.gam$smooth contains information about whether the knots have been placed; x.gam$coefficients gives the spline coefficients

Error in model.frame.default: variable lengths differ

给你一囗甜甜゛ 提交于 2019-11-27 14:01:25
On running a gam model using the mgcv package, I encountered a strange error message which I am unable to understand: “Error in model.frame.default(formula = death ~ pm10 + Lag(resid1, 1) + : variable lengths differ (found for 'Lag(resid1, 1)')”. The number of observations used in model1 is exactly the same as the length of the deviance residual, thus I think this error is not related to difference in data size or length. I found a fairly related error message on the web here , but that post did not receive an adequate answer, so it is not helpful to my problem. Reproducible example and data