如何在R中建立一个具有一个自变量和三个因变量的线性回归模型?

时间:2018-06-06 17:15:36

标签: r linear-regression

我有一个独立变量x和三个因变量y1y2y3。我想知道如何在R中建立线性回归模型?谢谢你的帮助。

enter image description here

抱歉这个令人困惑的表情。 y1y2y3是因变量,我只需要一条直线来构建线性关系。最终的解决方案应该是这样的:

enter image description here

set.seed(88)
x = sample(x=(-3:3), size = 20, replace = T)

y1= sample(x=(5:10), size = 20, replace = T)
y2= sample(x=(10:15), size = 20, replace = T)
y3= sample(x=(5:15), size = 20, replace = T)

plot(x,y1, ylim = c(5,15))
points(x,y2, col="red")
points(x,y3, col="blue")

2 个答案:

答案 0 :(得分:2)

多因变量的回归称为多元回归,在某些学科中很常见,在其他学科中很常见。可以在基础R中获得简单的线性模型:

df <- data.frame(x, y1, y2, y3)  #bind your observations into a dataframe
lm(cbind(y1,y2,y3) ~ x, data = df) #run an linear fit

请注意,统计学家对多元回归的最佳实施存在相当大的争议。有关详细信息,请转到此处:https://stats.stackexchange.com/questions/11127/multivariate-multiple-regression-in-r

答案 1 :(得分:0)

lm是你正在寻找的功能,不过首先你必须将你所有的y变量捆绑到一个变量中,并使用它们各自的x变量

> y4 = c(y1,y2,y3)
> x1 = rep(x,3)
> 
> model = lm(y4 ~ x1)
> 
> summary(model)

Call:
lm(formula = y4 ~ x1)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.9174 -1.1715  0.0799  2.0743  5.0771 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  9.917363   0.296917   33.40   <2e-16 ***
x1          -0.002786   0.139197   -0.02    0.984    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.284 on 58 degrees of freedom
Multiple R-squared:  6.904e-06, Adjusted R-squared:  -0.01723 
F-statistic: 0.0004005 on 1 and 58 DF,  p-value: 0.9841
相关问题