代码之家  ›  专栏  ›  技术社区  ›  Yang Yang

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

  •  -1
  • Yang Yang  · 技术社区  · 8 年前

    我有一个自变量 x 三个因变量 y1 , y2 y3 . 我想知道如何在r中建立一个线性回归模型?谢谢你的帮助。

    enter image description here

    很抱歉,这个表达很混乱。 Y1 , Y2 Y3 是因变量,我只需要一条直线来建立线性关系。最终的解决方案应该是这样的:

    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 回复  |  直到 8 年前
        1
  •  0
  •   Luis    8 年前

    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
    
        2
  •  2
  •   Nicolás Velasquez    8 年前

    多元因变量回归被称为多元回归,在某些学科中很常见,在其他学科中很少见。一个简单的线性模型可以通过以下方法在基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