簡體   English   中英

如何從GAM(`mgcv :: gam`)中提取擬合的樣條曲線

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

我正在使用GAM在Logistic回歸中對時間趨勢進行建模。 但是我想從中提取擬合的樣條,以將其添加到GAM或GAMM無法擬合的另一個模型中。

因此,我有兩個問題:

  1. 我如何才能隨着時間的推移安裝更平滑的鞋,以便在使模型找到其他結時強迫一個結位於特定位置?

  2. 如何從擬合的GAM中提取矩陣,以便可以將其用作其他模型的估算值?

我正在運行的模型類型為以下形式:

gam <- gam(mortality.under.2~ maternal_age_c+ I(maternal_age_c^2)+
           s(birth_year,by=wealth2) + wealth2 + sex +
           residence + maternal_educ + birth_order,
           data=colombia2, family="binomial")

我已經閱讀了GAM的詳盡文檔,但仍不確定。 任何建議都非常感謝。

mgcv::gam是有辦法做到這一點(您Q2),通過predict.gam方法和type = "lpmatrix"

?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)

 newd <- data.frame(x0=(0:30)/30, x1=(0:30)/30, x2=(0:30)/30, x3=(0:30)/30)

 Xp <- predict(b, newd, type="lpmatrix")

 ##################################################################
 ## The following shows how to use use an "lpmatrix" as a lookup 
 ## table for approximate prediction. The idea is to create 
 ## approximate prediction matrix rows by appropriate linear 
 ## interpolation of an existing prediction matrix. The additivity 
 ## of a GAM makes this possible. 
 ## There is no reason to ever do this in R, but the following 
 ## code provides a useful template for predicting from a fitted 
 ## gam *outside* R: all that is needed is the coefficient vector 
 ## and the prediction matrix. Use larger `Xp'/ smaller `dx' and/or 
 ## higher order interpolation for higher accuracy.  
 ###################################################################

 xn <- c(.341,.122,.476,.981) ## want prediction at these values
 x0 <- 1         ## intercept column
 dx <- 1/30      ## covariate spacing in `newd'
 for (j in 0:2) { ## loop through smooth terms
   cols <- 1+j*9 +1:9      ## relevant cols of Xp
   i <- floor(xn[j+1]*30)  ## find relevant rows of Xp
   w1 <- (xn[j+1]-i*dx)/dx ## interpolation weights
   ## find approx. predict matrix row portion, by interpolation
   x0 <- c(x0,Xp[i+2,cols]*w1 + Xp[i+1,cols]*(1-w1))
 }
 dim(x0)<-c(1,28) 
 fv <- x0%*%coef(b) + xn[4];fv    ## evaluate and add offset
 se <- sqrt(x0%*%b$Vp%*%t(x0));se ## get standard error
 ## compare to normal prediction
 predict(b,newdata=data.frame(x0=xn[1],x1=xn[2],
         x2=xn[3],x3=xn[4]),se=TRUE)

這甚至要經過整個過程,甚至是要在R或GAM模型之外完成的預測步驟。 您將需要對示例進行一些修改以執行所需的操作,因為該示例評估了模型中的所有項,並且除了樣條線外還有其他兩個項-本質上,您執行相同的操作,但僅針對樣條線項,涉及找到樣條的Xp矩陣的相關列和行。 然后,您還應注意,樣條線居中,因此您可能會也可能不想撤消該操作。

對於Q1,在示例中為xn向量/矩陣選擇適當的值。 這些對應於模型中第n個項的值。 因此,將要固定的值設置為某個平均值,然后更改與樣條曲線關聯的值。

如果您 R中執行所有這些操作,則僅使用樣條協變量的值來評估樣條就容易得多,因為該樣條的協變量的數據已進入另一個模型。 您可以通過創建一個數值框架來進行預測,然后使用

predict(mod, newdata = newdat, type = "terms")

其中mod是擬合的GAM模型(通過mgcv::gam ), newdat是數據幀,其中包含該模型中每個變量的列(包括參數項;將您不希望變化的項設置為某個恆定平均值) [說出數據集中變量的平均值]或一定水平(如果有因素的話)。 type = "terms"部分將為newdat每一行返回一個矩陣,其中包括對模型中各術語(包括樣條項)的擬合值的“貢獻”。 只需選取與樣條線對應的該矩陣的列-使其再次居中即可。

也許我誤解了您的Q1。 如果要控制結,請參見mgcv::gamknots參數。 默認情況下, mgcv::gam在數據的極端處打一個結,然后剩余的“結”在時間間隔內均勻分布。 mgcv::gam 找不到結-它為您放置了結,您可以通過knots參數控制它的放置位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM