How to only print (adjusted) R-squared of regression model?

后端 未结 1 1190
日久生厌
日久生厌 2021-01-28 06:22

I am a beginner with R. I have a data set on air pollution. The columns are site, measured concentration and 80 variables (v1-v80) that might influence the concentration. I wan

相关标签:
1条回答
  • 2021-01-28 06:42
    library(broom)
    glance(model)[c(1,2)]
    
    Input = ("site concentration         v1         v2         v3
              1    1   -0.84085548  1.7114409 -0.2857736 -1.0803926
              2    2    1.38435934 -0.6029080  0.1381082 -0.1575344
              3    3   -1.25549186 -0.4721664  1.2276303 -1.0717600")
    
    df = read.table(textConnection(Input),header=TRUE)
    
    for (j in names(df)){
        model <- lm(concentration ~ df[[j]], data = df)
        print(j)
        print(glance(model)[c(1,2)])
    }
    
    [1] "site"
        r.squared adj.r.squared
     1 0.02132635    -0.9573473
    [1] "concentration"
        r.squared adj.r.squared
      1         1             1
    [1] "v1"
      r.squared adj.r.squared
    1 0.1717716    -0.6564568
    [1] "v2"
      r.squared adj.r.squared
    1 0.1482473    -0.7035055
    [1] "v3"
      r.squared adj.r.squared
    1 0.9762587     0.9525174
    Warning message:
      In stats::summary.lm(x) :
      essentially perfect fit: summary may be unreliable
    

    Using base R

    summary(model)$adj.r.squared
    summary(model)$r.squared
    
    0 讨论(0)
提交回复
热议问题