[英]Regression Model (Outputs with only meaninful predictors)
I have constructed a linear regression model, reg_model1, and the model has factors within it.我已经构建了一个线性回归 model、reg_model1 和 model 中的因素。 However, within the different sets of factors in the model, very few are significant along with other continuous variables.然而,在 model 的不同组因子中,很少有与其他连续变量一起显着的。 Is there any code that one can supply to the reg_model1 to produce a summary that outputs only predictors that best fits the model?是否有任何代码可以提供给 reg_model1 以生成仅输出最适合 model 的预测变量的摘要?
From a statistical point of view I think you are making confusion between independent variables influencing the dependent variable and goodness of fit of the model, so my advice is to be sure about what you are trying to obtain.从统计的角度来看,我认为您在影响因变量的自变量与 model 的拟合优度之间存在混淆,因此我的建议是确定您想要获得的内容。 That said, if you want a representation of your model that only includes some of the variables, you may transform it into a dataframe with broom::tidy
:也就是说,如果您想要仅包含一些变量的 model 的表示形式,您可以使用broom::tidy
将其转换为 dataframe :
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
Created on 2021-11-20 by the reprex package (v2.0.1)由代表 package (v2.0.1) 于 2021 年 11 月 20 日创建
I'd suggest Stepwise Regression / Stepwise Selection.我建议逐步回归/逐步选择。 With this you can choose a best fit based on RSME and the goodness of fit.有了这个,您可以根据 RSME 和拟合优度选择最佳拟合。 Here's a good source performed on mtcars dataset.这是在mtcars数据集上执行的一个很好的来源。 There are several other packages which offer pretty much the same thing.还有其他几个包提供几乎相同的东西。 I personally prefer to use step function for this purpose.为此,我个人更喜欢使用步骤 function 。
step.model <- step(lm(mpg ~ ., mtcars), direction="both", trace=FALSE);
summary(step.model)
Another stepwise regression R package 'StepReg' you may use,另一个逐步回归 R package 'StepReg' 你可以使用,
For example,例如,
formula <- mpg ~ .
stepwise(formula=formula,
data=mtcars,
include="am",
selection="bidirection",
select="SL",
sle=0.15,
sls=0.15)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.