2016-10-21 1 views
1

`上のデータをスケッチするとき、私は(私はmgcvがより柔軟ですけど、私はここにgamを使用する必要があります)gamパッケージを使用してGAMに合うようにしようと見つけました。私は今モデルが良く見えるという問題を抱えていますが、元のデータと比較して、y軸に沿って一定の値でオフセットされているように見えます。`gam`パッケージ:余分なシフトがplot.gam`

このコードは、問題を再現:

library(gam) 
data(gam.data) 
x <- gam.data$x 
y <- gam.data$y 
fit <- gam(y ~ s(x,6)) 

fit$coefficients 
#(Intercept)  s(x, 6) 
# 1.921819 -2.318771 

plot(fit, ylim = range(y)) 
points(x, y) 
points(x, y -1.921819, col=2) 
legend("topright", pch=1, col=1:2, legend=c("Original", "Minus intercept")) 

enter image description here

チェン、JMおよびHastie、TJ Sにおける(1993)統計モデル(チャップマン&ホール)があってはならないことを示していますこれは直感的に正しい(スムーズにデータを記述する必要があります)。

mgcvに匹敵するものに気付きました。これは、shiftパラメータをモデルのインターセプト値に指定することで解決できます(スムースは一見中央に表示されるため)。私はここでも同じことが当てはまると思っていたので、元のデータポイントから切片を差し引いた。しかし、上のプロットはこの考え方を間違って示しています。私は余分なシフトがどこから来るのか分かりません。ここの誰かが私を助けてくれることを願っています。

(Rバージョン3.3.1; gamバージョン1.12。)

答えて

2

私は私が最初に取り付けGAMモデルでさまざまな出力を説明すべきだと思う:

library(gam) 
data(gam.data) 
x <- gam.data$x 
y <- gam.data$y 
fit <-gam(y ~ s(x,6), model = FALSE) 

## coefficients for parametric part 
## this includes intercept and null space of spline 
beta <- coef(fit) 

## null space of spline smooth (a linear term, just `x`) 
nullspace <- fit$smooth.frame[,1] 

nullspace - x ## all 0 

## smooth space that are penalized 
## note, the backfitting procedure guarantees that this is centred 
pensmooth <- fit$smooth[,1] 

sum(pensmooth) ## centred 
# [1] 5.89806e-17 

## estimated smooth function (null space + penalized space) 
smooth <- nullspace * beta[2] + pensmooth 

## centred smooth function (this is what `plot.gam` is going to plot) 
c0 <- mean(smooth) 
censmooth <- smooth - c0 

## additive predictors (this is just fitted values in Gaussian case) 
addpred <- beta[1] + smooth 

あなたが最初addpredは何fit$additive.predictorsであることを確認することができますGaussianレスポンスの加法モデルに適合しているので、これもfit$fitted.valuesと同じです。

plot.gam(fit, col = 4, ylim = c(-1.5,1.5)) 
points(x, censmooth, col = "gray") 

は、あなたがこのプロットに一致するように、元のデータyをシフトしたい場合は、あなたがする必要はありませのみ

addpred = beta[0] + censmooth + c0 

があり、覚えておいてください:censmoothをプロットすることである、何plot.gam

減算切片(beta[0])だけでなく、c0からy

points(x, y - beta[1] - c0) 

enter image description here

関連する問題