簡體   English   中英

從GAM平滑對象確定導數

[英]Determining derivatives from GAM smooth object

我有一個非常簡單的時間序列數據集,包括單個變量的年平均值(“AVERAGE”)。 我想調查時間序列的“趨勢”組成部分的變化率(一階導數)和加速度(二階導數)以及相關的標准誤差。 我使用MGCV的GAM和PREDICT功能獲得了“趨勢”,簡單如下:

A <- gam(AVERAGE ~ s(YEAR), data=DF, na.action=na.omit)
B <- predict(A, type="response", se.fit=TRUE)

我通過2種不同的方法確定了導數,應用了高DoF立方光滑樣條,並通過第一和第二差異(輕微平滑)和自舉來近似誤差,兩者都產生了可比較的結果。

我注意到“gam.fit3”功能有助於確定最多二階導數但不直接調用。 我還注意到使用類型為“lpmatrix”的“predict.gam”有助於平滑的衍生。 我想直接使用“GAM”函數計算第一和第二衍生物,但是不足以計算或提取這些衍生物。 我嘗試在“Predict.gam”幫助頁面的末尾重新配置Wood的示例,但是沒有成功。 讓我走向正確方向的任何幫助都會非常棒。 謝謝菲爾。

來自predict.gam的示例使用有限差分來近似平滑項的導數

以下是針對單個預測變量模型執行此操作的示例。 這個例子來自於幫助,這更直截了當。

A <- gam(AVERAGE ~ s(YEAR), data=DF, na.action=na.omit)
# new data for prediction
newDF <- with(DF, data.frame(YEAR = unique(YEAR)))
# prediction of smoothed estimates at each unique year value
# with standard error    
B <- predict(A,  newDF, type="response", se.fit=TRUE)


# finite difference approach to derivatives following
# example from ?predict.gam

eps <- 1e-7
X0 <- predict(A, newDF, type = 'lpmatrix')


newDFeps_p <- newDF + eps

X1 <- predict(A, newDFeps_p, type = 'lpmatrix')

# finite difference approximation of first derivative
# the design matrix
Xp <- (X0 - X1) / eps

# first derivative
fd_d1 <- Xp %*% coef(A)

# second derivative
newDFeps_m <- newDF - eps

X_1 <- predict(A, newDFeps_m, type = 'lpmatrix')
# design matrix for second derivative
Xpp <- (X1 + X_1 - 2*X0)  / eps^2
# second derivative
fd_d2 <- Xpp %*% coef(A)

如果使用引導捆綁來獲得置信區間,則應該能夠在這些近似值上獲得置信區間。

暫無
暫無

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

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