简体   繁体   中英

I have fitted an equation using GAMs (generalized additive models) in R using model1 <- mgcv::gam( x1..xn). How do i get the equation format?

library(mgcv)
model1 <- gam(x1,x2.....,xn)

I can get the partial dependency plots with the predictors and can also use the predict function for model prediction. But I want to publish the model in a Research journal, hence i will be needing the model in an equation format to be published in a paper so that someone reading the article can use the model to predict some element as the R file cannot be shared with the journal. Any help to get the GAM model as an equation like f(x1,x2..xn) = intercept+ coef1 * s(x1)+...+ coefn * s(xn). and what the the exact mathematical function of the spline function?

Fitting a non-linear model GAM shows good fit but unable to provide me with an equation.

The whole point of these models is that we typically don't have the mathematical equation for the fitted function. Of course, at some level we do have a mathematical equation but it will be written in terms of the basis expansion of the smooth covariate(s).

What we don't have is an equation of the form

$$ \hat{y}_i = \alpha + \beta_1 s_1(x_{1i}) + \beta_2 s_2(x_{2i}) + \cdots $$ as that isn't even the model that you fitted - you are not multiplying a smooth function ( $s_j()$ ) by a model parameter $\beta_j$ . What you typically fit with a GAM is something like:

$$ \hat{y}_i = \alpha + \sum_{k=1}^{K_1} { \beta_{1k} b_1(x_{1i}) } + \sum_{k=1}^{K_2} { \beta_{2k} b_2(x_{2i}) } + \cdots $$

where the $b_j$ are basis function of a particular type (low-rank thinplate regression spline, cubic regression spline, b spline, ...) and their associated coefficient ( $\beta_{jk}$ ) of which there will be $K_j$ such basis functions and coefficient pairs per smooth function $s_j()$ .

The pseudo-code you show ( gam(x1, x2, ..., xn) ) simply doesn't make any sense as a valid GAM model fitted with gam() : if should be of the form

gam(y ~ s(x1) + s(x2) + ... + s(xn),
    data = foo, method = "REML", family = fam, ...)

so it's not even clear what form of GAM you fitted.

Options for sharing your model are:

  1. Share the original data and the code used to fit the model, plus any specific version info for the software you used.

  2. Share the fitted model by serializing it to disk as an RDS object via saveRDS() plus code to show how to use it to predict (this is actually not such a simple thing as factors need to have the right levels etc, if you have a more complex model).

  3. You could pre-compute what is know as the $L_p$ matrix or linear predictor matrix for your model. Say you have a simple model of the form:

     m <- gam(y ~ s(x1), data = foo, ...)

    then you would generate new values for x1 over a fine grid (as fine a grid as you would need for the practical and scientific purposes you might want to use the model at, and certainly no finer than the precision of x1 ), and create the $L_p$ matrix via the predict() method:

     newd <- data.frame(x1 = seq(min(x1), max(x1), by = 0.1)) # for example Xp <- predict(m, newdata = newd, type = "lpmatrix")

    You will also need to extract the model coefficients

    betas <- coef(m)

    And save all of these ( newd , Xp and betas ) to disk via saveRDS() .

    The new user can then assign each of the values in their data to a row in newd (to the closest value in newd ), extract those same rows from Xp , and then generate predictions/fitted values via

    fit <- Xp[want, ] %*% betas

    which would yield predicted values on the link scale, which you could transform to the response scale if needed by application of the inverse of the relevant link function.

  4. There's also possibly something you might be able to do with Predictive Model Markup Language (PMML) but I am not familiar with this functionality in R or for {mgcv} GAMs in particular.

Which of these methods works best will depend on the intended use of readers of your paper, with option 1 being the most broadly useful way to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM