[英]How to interpret lm() coefficient estimates when using bs() function for splines
我正在使用一組從(-5,5)
到(0,0)
和(5,5)
,呈“對稱V形”。 我正在用lm()
和bs()
函數擬合模型以擬合“ V形”樣條線:
lm(formula = y ~ bs(x, degree = 1, knots = c(0)))
當我通過predict()
預測結果並繪制預測線時,我得到“ V形”。 但是,當我查看模型估計coef()
,會看到我沒有想到的估計。
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.93821 0.16117 30.639 1.40e-09 ***
bs(x, degree = 1, knots = c(0))1 -5.12079 0.24026 -21.313 2.47e-08 ***
bs(x, degree = 1, knots = c(0))2 -0.05545 0.21701 -0.256 0.805
我希望第一部分的系數為-1
,第二部分的系數為+1
。 我是否必須以其他方式解釋估算值?
如果我手動在lm()
函數中填充結,則得到以下系數:
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.18258 0.13558 -1.347 0.215
x -1.02416 0.04805 -21.313 2.47e-08 ***
z 2.03723 0.08575 23.759 1.05e-08 ***
這還差不多。 Z對x的結點相對變化為+1
我想了解如何解釋bs()
結果。 我已經檢查過,手冊和bs
模型的預測值完全相同。
我希望第一部分的系數為
-1
,第二部分的系數為+1
。
我認為您的問題確實是關於什么是B樣條函數 。 如果要了解系數的含義,則需要知道樣條曲線的基函數是什么。 請參閱以下內容:
library(splines)
x <- seq(-5, 5, length = 100)
b <- bs(x, degree = 1, knots = 0) ## returns a basis matrix
str(b) ## check structure
b1 <- b[, 1] ## basis 1
b2 <- b[, 2] ## basis 2
par(mfrow = c(1, 2))
plot(x, b1, type = "l", main = "basis 1: b1")
plot(x, b2, type = "l", main = "basis 2: b2")
注意:
b1
可以看出,度為1的B樣條是帳篷函數 。 (0, 1)
; 您可以從B樣條的定義中獲得B樣條的(遞歸)表達式。 0度的B樣條是最基類,而
(對不起,我沒話題了...)
使用B樣條曲線的線性回歸:
y ~ bs(x, degree = 1, knots = 0)
只是在做:
y ~ b1 + b2
現在,您應該能夠理解所獲得的平均系數,這意味着樣條函數為:
-5.12079 * b1 - 0.05545 * b2
在匯總表中:
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.93821 0.16117 30.639 1.40e-09 ***
bs(x, degree = 1, knots = c(0))1 -5.12079 0.24026 -21.313 2.47e-08 ***
bs(x, degree = 1, knots = c(0))2 -0.05545 0.21701 -0.256 0.805
您可能想知道為什么b2
的系數不重要。 好吧,比較您的y
和b1
:您的y
是對稱V形 ,而b1
是反向對稱V形 。 如果首先將-1
乘以b1
,然后乘以5重新縮放(這解釋了b1
的系數-5
),那么您會得到什么? 很好的搭配,對不對? 因此,不需要b2
。
但是,如果y
是不對稱的,則將(-5,5)
波谷從(0,0)
擴展到(5,10)
,然后您會注意到b1
和b2
系數都很重要。 我認為其他答案已經給您提供了這樣的例子。
擬合的B樣條曲線到分段多項式的重新參數化在此處得到證明: 擬合的回歸樣條曲線作為分段多項式和輸出多項式系數的重新參數化 。
具有單結的一級樣條的簡單示例,並解釋了估計系數以計算擬合線的斜率 :
library(splines)
set.seed(313)
x<-seq(-5,+5,len=1000)
y<-c(seq(5,0,len=500)+rnorm(500,0,0.25),
seq(0,10,len=500)+rnorm(500,0,0.25))
plot(x,y, xlim = c(-6,+6), ylim = c(0,+8))
fit <- lm(formula = y ~ bs(x, degree = 1, knots = c(0)))
x.predict <- seq(-2.5,+2.5,len = 100)
lines(x.predict, predict(fit, data.frame(x = x.predict)), col =2, lwd = 2)
產生情節 由於我們擬合的樣條曲線的
degree=1
(即直線),且結點在x=0
,因此對於x<=0
和x>0
,我們有兩條線。
系數是
> round(summary(fit)$coefficients,3)
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.014 0.021 241.961 0
bs(x, degree = 1, knots = c(0))1 -5.041 0.030 -166.156 0
bs(x, degree = 1, knots = c(0))2 4.964 0.027 182.915 0
可以使用結(我們在x=0
處指定)和邊界結(說明數據的最小值/最大值)將其轉換為每個直線的斜率 :
# two boundary knots and one specified
knot.boundary.left <- min(x)
knot <- 0
knot.boundary.right <- max(x)
slope.1 <- summary(fit)$coefficients[2,1] /(knot - knot.boundary.left)
slope.2 <- (summary(fit)$coefficients[3,1] - summary(fit)$coefficients[2,1]) / (knot.boundary.right - knot)
slope.1
slope.2
> slope.1
[1] -1.008238
> slope.2
[1] 2.000988
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.