R 在绘图本身上打印线性回归方程
我们如何在绘图上打印直线的方程式 我有两个自变量,想要一个这样的方程:R 在绘图本身上打印线性回归方程,r,regression,R,Regression,我们如何在绘图上打印直线的方程式 我有两个自变量,想要一个这样的方程: y=mx1+bx2+c where x1=cost, x2 =targeting 我可以绘制最佳拟合线,但如何在绘图上打印方程式 也许我不能在一个等式中打印出两个自变量,但是我该怎么做呢 y=mx1+c至少 这是我的密码: fit=lm(Signups ~ cost + targeting) plot(cost, Signups, xlab="cost", ylab="Signups", main="Signups")
y=mx1+bx2+c
where x1=cost, x2 =targeting
我可以绘制最佳拟合线,但如何在绘图上打印方程式
也许我不能在一个等式中打印出两个自变量,但是我该怎么做呢
y=mx1+c
至少
这是我的密码:
fit=lm(Signups ~ cost + targeting)
plot(cost, Signups, xlab="cost", ylab="Signups", main="Signups")
abline(lm(Signups ~ cost))
你用。此外,您不应该使用abline(lm(Signups~cost))
,因为这是一种不同的模式(请参见我在简历中的答案:)。无论如何,请考虑:
set.seed(1)
Signups <- rnorm(20)
cost <- rnorm(20)
targeting <- rnorm(20)
fit <- lm(Signups ~ cost + targeting)
summary(fit)
# ...
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.1494 0.2072 0.721 0.481
# cost -0.1516 0.2504 -0.605 0.553
# targeting 0.2894 0.2695 1.074 0.298
# ...
windows();{
plot(cost, Signups, xlab="cost", ylab="Signups", main="Signups")
abline(coef(fit)[1:2])
text(-2, -2, adj=c(0,0), labels="Signups = .15 -.15cost + .29targeting")
}
set.seed(1)
注册我尝试将输出自动化一点:
fit <- lm(mpg ~ cyl + hp, data = mtcars)
summary(fit)
##Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 36.90833 2.19080 16.847 < 2e-16 ***
## cyl -2.26469 0.57589 -3.933 0.00048 ***
## hp -0.01912 0.01500 -1.275 0.21253
plot(mpg ~ cyl, data = mtcars, xlab = "Cylinders", ylab = "Miles per gallon")
abline(coef(fit)[1:2])
## rounded coefficients for better output
cf <- round(coef(fit), 2)
## sign check to avoid having plus followed by minus for negative coefficients
eq <- paste0("mpg = ", cf[1],
ifelse(sign(cf[2])==1, " + ", " - "), abs(cf[2]), " cyl ",
ifelse(sign(cf[3])==1, " + ", " - "), abs(cf[3]), " hp")
## printing of the equation
mtext(eq, 3, line=-2)
fit | t |)
##(截距)36.90833 2.19080 16.847<2e-16***
##气缸-2.26469 0.57589-3.933 0.00048***
##hp-0.01912 0.01500-1.275 0.21253
绘图(mpg~cyl,数据=mtcars,xlab=“气缸”,ylab=“每加仑英里”)
abline(coef(fit)[1:2])
##四舍五入系数以获得更好的输出
cf这里有一个使用tidyverse
包的解决方案
关键是broom
包,它简化了提取模型数据的过程。例如:
fit1 <- lm(mpg ~ cyl, data = mtcars)
summary(fit1)
fit1 %>%
tidy() %>%
select(estimate, term)
fit2 <- lm(mpg ~ cyl + disp + wt + hp, data = mtcars)
summary(fit2)
fit2 %>%
tidy() %>%
select(estimate, term)
结果
# A tibble: 2 x 2
estimate term
<dbl> <chr>
1 37.9 (Intercept)
2 -2.88 cyl
[1] " 37.88 - 2.88 cyl"
# A tibble: 5 x 2
estimate term
<dbl> <chr>
1 40.8 (Intercept)
2 -1.29 cyl
3 0.0116 disp
4 -3.85 wt
5 -0.0205 hp
然后使用ggplot2
绘制线条并添加标题
mtcars %>%
ggplot(mapping = aes(x = cyl, y = mpg)) +
geom_point() +
geom_smooth(formula = y ~ x, method = "lm", se = FALSE) +
labs(
x = "Cylinders", y = "Miles per Gallon",
caption = paste("mpg =", get_formula(fit1))
)
这种绘制直线的方法实际上只有在可视化两个变量之间的关系时才有意义。正如@Glen_b在评论中指出的,我们从建模mpg
得到的斜率作为cyl
的函数(-2.88),与我们从建模mpg
得到的斜率作为cyl
和其他变量的函数(-1.29)不匹配。例如:
fit1 <- lm(mpg ~ cyl, data = mtcars)
summary(fit1)
fit1 %>%
tidy() %>%
select(estimate, term)
fit2 <- lm(mpg ~ cyl + disp + wt + hp, data = mtcars)
summary(fit2)
fit2 %>%
tidy() %>%
select(estimate, term)
1)您是想要方程式中的系数值,还是只想要y=mx1+bx2+c
?2) 绘制的直线(1个预测值)与拟合的线性模型不对应。事实上,直线上的成本变量系数与多元回归中的系数相吻合。如果你打印的东西可能完全不同,这会不会让人困惑?重复:(ggplot),(二次型),(多面ggplot),(格子型)