R 在图本身上打印线性回归方程

编程入门 行业动态 更新时间:2024-10-27 00:34:23
本文介绍了R 在图本身上打印线性回归方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们如何在图上打印一条线的方程?

How do we print the equation of a line on a plot?

我有 2 个自变量,想要一个这样的方程:

I have 2 independent variables and would like an equation like this:

y=mx1+bx2+c where x1=cost, x2 =targeting

我可以绘制最佳拟合线,但如何在图中打印方程?

I can plot the best fit line but how do i print the equation on the plot?

也许我不能在一个方程中打印 2 个自变量,但我该怎么做呢?y=mx1+c 至少?

Maybe i cant print the 2 independent variables in one equation but how do i do it for say y=mx1+c at least?

这是我的代码:

fit=lm(Signups ~ cost + targeting) plot(cost, Signups, xlab="cost", ylab="Signups", main="Signups") abline(lm(Signups ~ cost))

推荐答案

我尝试将输出自动化一点:

I tried to automate the output a bit:

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)

希望能帮到你,

亚历克斯

更多推荐

R 在图本身上打印线性回归方程

本文发布于:2023-11-30 14:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线性   方程   身上

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!