我对一些可以从RDD中受益的数据进行回归。因此,我想在x轴上的阈值0.5以上和以下显示最佳拟合/回归线。
我正在努力做到这一点。我已经尝试了clip(x1,x2,y1,y2)
命令,但它仍然在整个情节中绘制线条。我还尝试使用子集绘制回归线> /< 0.5,它也在整个图中给出一条线。
使用lowess线路可能会更好吗?这对我来说真的是一个未知的R领域,所以我真的不确定如何继续。
答案 0 :(得分: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)
或者,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])