[英]How to extract the underlying coefficients from fitting a linear b spline regression in R?
[英]How to extract the slope of the lines before and after the knot point when fitting a spline regression?
目標:我希望在樣條回歸(分段線性)model中提取結點前后的直線斜率(即提取拐點前后的兩個線性模型)。
示例 dataframe,其中在 15 個會話中記錄了 DV(平均塊級別):
structure(list(subject = c("participant_003", "participant_003", "participant_003", "participant_003", "participant_003", "participant_003", "participant_003", "participant_003", "participant_003", "participant_003", "participant_003","participant_003", "participant_003", "participant_003", "participant_003"), group_no = c("group1", "group1", "group1","group1", "group1", "group1", "group1", "group1", "group1", "group1","group1", "group1", "group1", "group1", "group1"), session = 1:15,mean_block_level = c(1.3, 1.2, 1.6, 1.8, 1.6, 1.9, 2.2, 2,
1.8, 1.9, 2.2, 2.1, 1.9, 1.9, 2)), class = "data.frame", row.names = c(NA,-15L))
我使用以下代碼在 session 7 處安裝了一個帶有單個結點的樣條回歸(注意,我沒有使用任何與 package 相關的“樣條”來實現此目的):
df$X_bar <- ifelse(df$session>7,1,0)
df$diff <- df$session - 7
df$X <- df3$diff*df$X_bar
df
reg <- summary(lm(mean_block_level~ session + X, data = df))
summary(reg)
reg <-lm(mean_block_level~ session + X, data = df)
plot(mean_block_level ~ session, df)
lines(df$session, predict(reg), col = 'green')
關於這個主題的現有帖子傾向於使用不同的包來創建他們的樣條模型,所以不完全回答我的問題(例如,
https://stackoverflow.com/questions/29499686/how-to-extract-the-underlying-coefficients-from-fitting-a-linear-b-spline-regres
您可以從 model 系數中獲取斜率:第一部分為coef(reg)[2]
,第二部分為sum(coef(reg)[2:3])
。
您的 model 具有三個擬合系數:
coef(reg)
# (Intercept) session X
# 1.1095238 0.1321429 -0.1416667
前兩個(截距、會話)為您提供第一部分的截距和斜率,如果您將第三個 (X) 添加到第二個,您將獲得第二部分的斜率。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.