【问题标题】:Plotting regression using abline in R在 R 中使用 abline 绘制回归
【发布时间】:2021-07-09 00:57:06
【问题描述】:

您好,我正在尝试使用 abline 在我的散点图中画一条线,我尝试了几种不同的方法,但我不太确定我做错了什么! (对 R 来说相当新)

编辑:我也尝试过的代码

plot(data$GRE.Score, data$Chance.of.Admit, main = "Regression Line plot", 
     xlab = "Chance of Admit", ylab = "GRE Score", 
     pch = 19, frame = FALSE)

abline(lm(GRE.Score ~ Chance.of.Admit, data = data), col = "red")

【问题讨论】:

标签: r plot regression


【解决方案1】:

我希望这个小例子能指导你:

# dummy data
df <- data.frame(x = 1:100, y = rpois(100, lambda = 4))

# plot
plot(df$x, df$y, main = "Main title",
     xlab = "X axis title", ylab = "Y axis title",
     pch = 19, frame = FALSE)

# linear regression line (can only be called AFTER the plot call)
abline(lm(y ~ x, data = df), col = "blue")
# vertical line that cuts X at 10
abline(v = 10, col = "red")
# horizontal line that cuts Y at 10
abline(h = 10, col = "green")

使用 kaggle 数据:

df <- read.csv("C:/.../Admission_Predict.csv")
# plot
plot(df$GRE.Score, df$Chance.of.Admit, main = "Main title",
     xlab = "X axis title", ylab= "Y axis title",
     pch = 19, frame = FALSE)
abline(lm(Chance.of.Admit ~ GRE.Score, data = df), col = "red")

【讨论】:

  • 您好,这是我的代码;我已经按照您的布局方式进行了尝试 x
  • 我猜 data 对你来说不是一个很好的标识符...使用 df 代替...我刚刚运行了这段代码(我们对虚拟数据稍作修改): plot(df$x, df $y, main = "回归线图", xlab = "录取机会", ylab = "GRE 分数", pch = 19, frame = FALSE) abline(lm(y ~ x, data = df), col = "红色")
  • 不幸的是,现在它给了我“model.frame.default 中的错误(formula = y ~ x, data = df, drop.unused.levels = TRUE):'data' 必须是数据。框架、环境或列表”
  • 我想没有办法围绕一个可重现的例子(错误应该在你的数据中,但我只能猜测而无法重现):stackoverflow.com/questions/5963269/…
  • 是的,这很奇怪,没有 abline() 它完全可以正常工作,但是当我尝试按顺序运行它们时,包括 abline() 出现了问题!非常感谢您提供帮助!我想我现在就跳过这个!
猜你喜欢
  • 2017-06-12
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 2017-02-05
  • 2014-04-22
  • 1970-01-01
  • 2018-09-04
  • 2022-12-03
相关资源
最近更新 更多