最佳拟合线与R中的阈值

问题描述 投票:0回答:1

我正在对一些可以从RDD中受益的数据进行回归。因此,我想在x轴上的阈值0.5以上和以下显示最佳拟合/回归线。我正在努力做到这一点。我已经尝试过clip(x1,x2,y1,y2)命令,但它仍然在整个情节中绘制线条。我还尝试使用子集绘制回归线> / <0.5,这也给出了整个图的一条线。

用lowess线路可能会更好吗?这对我来说真的是一个未知的R领域,所以我真的不确定如何继续。

r statistics
1个回答
0
投票

没有示例数据集,很难说哪种方法最适合你,但你可以考虑geom_smooth中的ggplot

library(ggplot2)

# simulated data
set.seed(123)
beta_low <- -3; beta_high <- 2
cut_off <- 0.5 
x = seq(0,1, by = 0.01)
y <- ifelse(x < cut_off, x*beta_low, x*beta_high) + rnorm(length(x),     
                                                    mean = 0, sd = 2)

# add a new variable, say "group", which indicates whether you're before the 
# cut-off or after
df <- data.frame(x, y, group = ifelse(x < cut_off, "before_cut", 
"after_cut"))

# geom_smooth in combination with the group argument in aes() will make sure 
# that lines are only shown for the specified regions >/< 0.5
ggplot(df, aes(x, y, group = group)) +
geom_point() + 
geom_smooth(method = "lm", fill = NA, fullrange = FALSE)

enter image description here

或者,base R解决方案:

part1 <- lm(y ~ x, subset=(x<=cut_off))
part2 <- lm(y ~ x, subset=(x>cut_off))
plot(x,y)
segments(min(x), part1$fitted.values[1],                             
         cut_off, rev(part1$fitted.values)[1])
segments(cut_off, part2$fitted.values[1],                             
         max(x), rev(part2$fitted.values)[1])

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.