【问题标题】:Plotting linear regression line of a calculation绘制计算的线性回归线
【发布时间】:2019-06-29 04:52:24
【问题描述】:

我正在尝试为以下问题绘制线性回归线。如果第一列是住在一个房间里的狗的数量,第二列代表每只狗可以抓取的食物量,那么当有 10 只狗和 15 只狗时,估计每只狗可以抓取的食物量分别是多少,在房间里?我需要编写一个函数来计算给定 x 向量的估计值 y 向量。用“o”型点画出实际值,用“+”型点画出估计值。您还需要绘制回归线。)

提示使用如下:

lmout <- lm (y ~ x)

intercept <- lmout[1]$coefficients[[1]]
constant <- lmout[1]$coefficients[[2]]

我不知道我需要根据问题计算什么。如果给定的矩阵如下所示,我不明白需要什么:

  Number of dogs in a room Amount of food each dog can grab
1                        8                               12
2                       20                               15
3                       10                                2

问题要求计算当每个房间分别有 10 只狗和 15 只狗时,每只狗可以抓取多少食物? 到目前为止,我所做的是绘制矩阵和回归线的值。

rownames = c("1","2","3") #Declaring row names
colnames = c("Number of dogs in a room", "Amount of food each dog can grab") #Declaring column names

v <- matrix(c(8,12,20,15,10,2), nrow = 3, byrow=TRUE, dimnames = list(rownames,colnames))

print(v) # Prints the matrix of the data

# Data in vector form
x <- c(8,20,10)
y <- c(12,15,2)

# calculate linear model
lmout <- lm (y ~ x)
# plot the data
plot(x,y, pch =19)
# plot linear regression line
abline(lmout, lty="solid", col="royalblue")

# Function
func <- function(lmout,x){

  intercept <- lmout[1]$coefficients[[1]]
  constant <- lmout[1]$coefficients[[2]]

  regline2 <- lm(intercept, constant) 
  abline(regline2, lty="solid", col="red")

}

print(func(lmout,x))

【问题讨论】:

  • 也许您想尝试查看str(lmout),它向您展示了lm 输出的结构,并且可能已经包含一些您感兴趣的值。也许lmout$fitted.values 对你有特别的兴趣?

标签: r plot linear-regression


【解决方案1】:

听起来您想要每个房间 10 和 15 只狗的预测值 food。你可以通过predict 做到这一点。首先,我会将矩阵转换为数据框,以使事情变得更容易:

# Turn you matrix into a dataframe.
df <- data.frame(dogs = v[,1], food = v[,2])

然后我可以根据模型计算我的模型和预测:

# Compute the linear model.
lmout <- lm(food ~ dogs, df)

# Create a dataframe with new values of `dogs`.
df_new <- data.frame(dogs = c(10, 15))

# Use `predict` with your model and the new data.
df_new$food <- predict(lmout, newdata = df_new)

#### PREDICTIONS OUTPUT ####

  dogs      food
1   10  8.096774
2   15 11.040323

现在我可以用回归线绘制数据和新数据。

plot(df$dogs, df$food, pch = 21)
abline(lmout, lty="solid", col="royalblue")
points(df_new$dogs, df_new$food, pch = 3, col = "red")

【讨论】:

  • @jay.sf 感谢您的链接。我不确定告诉somone 如何使用predict 是否有问题。几乎每个教程都做同样的事情。但我绝对明白我应该更加小心。
【解决方案2】:

由于这听起来像是家庭作业,我将向您展示如何仅使用 R 中的内置函数来执行此操作。您必须构建自己的函数才能动态执行此操作。如果您是老师希望您从头开始,请记住:

yhat = beta0 + beta1 * x # No LaTeX Support here?

dog_dat <- data.frame("dogs_room" = c(8, 20, 10), "food" = c(12, 15, 2))
dog.lm <- lm(dogs_room ~ food, data = dog_dat)

plot(dog_dat$food, dog_dat$dogs_room)
points(dog_dat$food, fitted.values(dog.lm), col = "red")
abline(dog.lm)

reprex package (v0.2.1) 于 2019 年 6 月 28 日创建

【讨论】:

  • 我不明白函数中需要进行什么计算? yhat = ?
  • @888 这是来自线性模型的系数。 beta0 = (Intercept)beta1 = x
  • 那么yhat= intercept + x * ?中的x是yhat = intercept + x * constant吗?
  • @888 抱歉,我知道这会让人感到困惑。两个 beta 都是模型的常数系数。 x 在这种情况下是您的数据“房间里的狗”。 yhat 是食物的估计值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 2021-03-18
  • 2021-11-23
  • 2018-07-10
  • 2012-01-12
  • 2014-02-20
  • 1970-01-01
相关资源
最近更新 更多