简单线性回归实例

  1. 用 lm 拟合线性模型

    1. 使用学生调查数据(MASS 包中的 survey 数据框), 如果没有加载所需软件包,调用 library("MASS")
      
       library("MASS")
      
        plot(survey$Height, survey$Wr.Hnd, xlab="Writing handspan (cm)", ylab="Height (cm) ")
      
          
      You will see the following result: 学生的手夸度和身高之间存在正相关
      
              cor(survey$Height, survey$Wr.Hnd, use="complete.obs")
      
      
      估计得到学生的手夸度和身高相关系数是 0.6009909。 用 lm 拟合线性模型
      
          survfit=lm(Height~Wr.Hnd, data=survey)
      
      
      可以得到 身高=113.954+3.117*手跨度
      
       > survfit
      
       Call:
      lm(formula = Height ~ Wr.Hnd, data = survey)
      
      Coefficients:
      (Intercept)       Wr.Hnd
          113.954        3.117