简体   繁体   English

如何从线性回归分析中计算斜率?

[英]How can I calculate the slope from a linear regression analysis?

In R, I am trying to overlay an abline onto a plot, the result of linear regression.在 R 中,我试图将 abline 覆盖到 plot 上,这是线性回归的结果。 I want to create a scatter plot showing TrainRegRpt$train.data.Price (original price) on the x-axis, TrainRegress$fitted.values (the projected price that came from the lm model) on the y-axis and draw the line of best fit through the plotted points.我想创建一个散点图 plot 在 x 轴上显示TrainRegRpt$train.data.Price (原价),在 y 轴上显示TrainRegress$fitted.values (来自 lm 模型的预计价格)并画线通过绘制点的最佳拟合。

Here is some of my code:这是我的一些代码:

TrainRegress <- lm(PriceBH.df$Price ~ ., data=PriceBH.df, subset = train.rows)
TrainRegRpt  <- data.frame(train.data$Price, TrainRegress$fitted.values, TrainRegress$residuals)
x <- as.vector(TrainRegRpt$TrainRegress.fitted.values)    # on the x-axis
y <- as.vector(TrainRegRpt$train.data.Price)     #on the y-axis
plot(TrainRegRpt$train.data.Price ~ TrainRegRpt$TrainRegress.fitted.values)
abline(x,y)

The scatter plot came out the same:散点图 plot 的结果是一样的:

x <- as.vector(newdf$fv)
y <- as.vector(newdf$p)
p <-as.vector(TrainRegRpt$train.data.Price) # my y-axis in the scatter plot
fv <- as.vector(round(TrainRegRpt$TrainRegress.fitted.values,2) # my y-axis in the scatter plot
newdf<- dfrm <- data.frame(p,fv)
plot(newdf$p ~ newdf$fv)
abline(x,y)
summary(TrainRegress)

The following is the summary of TrainRegress: Coefficients obtained from the summary of TrainRegress:以下是 TrainRegress 的摘要: 从 TrainRegress 的摘要中得到的系数:

Intercept  Estimate     
................30.318
CRIM.........0.245
CHAS......5.8368
RM..........8.4846

I extracted the y-intercept as follows:我提取了 y 截距如下:

y.interceptval <-summary(TrainRegress)$coefficients[1]

I will use y.interceptval in the abline(y.interceptval,***?slope***) but I need to know how to calculate the slope.我将在abline(y.interceptval,***?slope***)中使用y.interceptval ,但我需要知道如何计算斜率。 How do I calculate the slope to pass to abline(y.interceptval, slope) ?如何计算传递给abline(y.interceptval, slope)

I have 5 textbooks here that are no help and my professor refuses to help me and I really want this to be perfect!我这里有 5 本没有帮助的教科书,我的教授拒绝帮助我,我真的希望这本书完美无缺! Thank you!!!谢谢!!!

plot(TrainRegRpt$train.data.Price ~ TrainRegRpt$TrainRegress.fitted.values)<br>
abline(x,y)

阴谋

It looks like you already calculated your slope.看起来您已经计算了斜率。 The slopes from a linear regression analysis using lm() are the coefficients.使用lm()进行线性回归分析的斜率是系数。 So, in this case, 30.318 is your Y-intercept.因此,在这种情况下, 30.318是您的 Y 轴截距。

This gives you a regression equation of:这为您提供了一个回归方程:

Y = 30.318 + 0.245*(CRIM) + 5.8368*(CHAS) + 8.4846*(RM)

The numbers 0.245 , 5.8368 , and 8.4846 are the coefficients for each variable and they are also the individual slopes.数字0.2455.83688.4846是每个变量的系数,它们也是各个斜率。

Also, one thing about your fitted vs reesiduals plot, it looks like you reversed the way abline() is supposed to be (ie instead of abline(x,y) it should be abline(y,x) .此外,关于您的拟合与残差 plot 的一件事,看起来您颠倒了abline()应该是的方式(即,而不是abline(x,y)它应该是abline(y,x)

Edit You used abline(x,y) but your plotted data are编辑您使用abline(x,y)但您绘制的数据是

plot(TrainRegRpt$train.data.Price ~ TrainRegRpt$TrainRegress.fitted.values)

( train.data.Price vs. Fitted Values not x vs y ). train.data.Price vs. Fitted Values not x vs y )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM