【问题标题】:Regression Model (Outputs with only meaninful predictors)回归模型(只有有意义的预测变量的输出)
【发布时间】:2021-12-30 19:34:33
【问题描述】:

我已经构建了一个线性回归模型 reg_model1,该模型中包含因子。然而,在模型中的不同组因素中,很少有与其他连续变量一起显着的因素。是否有任何代码可以提供给 reg_model1 以生成仅输出最适合模型的预测变量的摘要?

【问题讨论】:

    标签: r lm coefficients best-fit coefficient-of-determination


    【解决方案1】:

    从统计的角度来看,我认为您在影响因变量和模型拟合优度的自变量之间存在混淆,因此我的建议是确定您想要获得的结果。也就是说,如果您想要一个只包含一些变量的模型表示,您可以将其转换为带有broom::tidy 的数据框:

    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    library(broom)
    
    ### Create factors ###
    mtcars <- mutate(mtcars, across(c(vs, am, gear), as.factor))
    
    lm(mpg ~ disp + vs + am + gear, data=mtcars) |> 
      tidy() |> 
      filter(p.value <= 0.05)
    #> # A tibble: 3 × 5
    #>   term        estimate std.error statistic      p.value
    #>   <chr>          <dbl>     <dbl>     <dbl>        <dbl>
    #> 1 (Intercept)  24.7      3.36         7.34 0.0000000865
    #> 2 disp         -0.0282   0.00924     -3.05 0.00518     
    #> 3 am1           4.67     2.09         2.23 0.0345
    

    reprex package (v2.0.1) 于 2021 年 11 月 20 日创建

    【讨论】:

      【解决方案2】:

      我建议逐步回归/逐步选择。有了这个,您可以根据 RSME 和拟合优度选择最佳拟合。这是在 mtcars 数据集上执行的good source。还有其他几个包提供几乎相同的东西。为此,我个人更喜欢使用step function

      step.model <- step(lm(mpg ~ ., mtcars), direction="both", trace=FALSE); 
      summary(step.model)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-05
        • 2021-09-25
        • 1970-01-01
        • 2019-03-27
        • 2020-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多