Remove Particular Predictors from GLM in R (Example Code)
This tutorial shows how to remove particular predictors from a GLM in R programming.
Creation of Example Data
data(iris) # Load example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa |
data(iris) # Load example data head(iris) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
model_a <- lm(Sepal.Length ~ ., iris) # Estimate linear regression model summary(model_a) # Display summary of model # Call: # lm(formula = Sepal.Length ~ ., data = iris) # # Residuals: # Min 1Q Median 3Q Max # -0.79424 -0.21874 0.00899 0.20255 0.73103 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 2.17127 0.27979 7.760 1.43e-12 *** # Sepal.Width 0.49589 0.08607 5.761 4.87e-08 *** # Petal.Length 0.82924 0.06853 12.101 < 2e-16 *** # Petal.Width -0.31516 0.15120 -2.084 0.03889 * # Speciesversicolor -0.72356 0.24017 -3.013 0.00306 ** # Speciesvirginica -1.02350 0.33373 -3.067 0.00258 ** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.3068 on 144 degrees of freedom # Multiple R-squared: 0.8673, Adjusted R-squared: 0.8627 # F-statistic: 188.3 on 5 and 144 DF, p-value: < 2.2e-16 |
model_a <- lm(Sepal.Length ~ ., iris) # Estimate linear regression model summary(model_a) # Display summary of model # Call: # lm(formula = Sepal.Length ~ ., data = iris) # # Residuals: # Min 1Q Median 3Q Max # -0.79424 -0.21874 0.00899 0.20255 0.73103 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 2.17127 0.27979 7.760 1.43e-12 *** # Sepal.Width 0.49589 0.08607 5.761 4.87e-08 *** # Petal.Length 0.82924 0.06853 12.101 < 2e-16 *** # Petal.Width -0.31516 0.15120 -2.084 0.03889 * # Speciesversicolor -0.72356 0.24017 -3.013 0.00306 ** # Speciesvirginica -1.02350 0.33373 -3.067 0.00258 ** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.3068 on 144 degrees of freedom # Multiple R-squared: 0.8673, Adjusted R-squared: 0.8627 # F-statistic: 188.3 on 5 and 144 DF, p-value: < 2.2e-16
Example: Remove Certain Predictor Variables from Linear Regression Model
model_b <- lm(Sepal.Length ~ . - Sepal.Width - Petal.Width, iris) # Exclude predictors from lm() summary(model_b) # Display summary of model # Call: # lm(formula = Sepal.Length ~ . - Sepal.Width - Petal.Width, data = iris) # # Residuals: # Min 1Q Median 3Q Max # -0.75310 -0.23142 -0.00081 0.23085 1.03100 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 3.68353 0.10610 34.719 < 2e-16 *** # Petal.Length 0.90456 0.06479 13.962 < 2e-16 *** # Speciesversicolor -1.60097 0.19347 -8.275 7.37e-14 *** # Speciesvirginica -2.11767 0.27346 -7.744 1.48e-12 *** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.338 on 146 degrees of freedom # Multiple R-squared: 0.8367, Adjusted R-squared: 0.8334 # F-statistic: 249.4 on 3 and 146 DF, p-value: < 2.2e-16 |
model_b <- lm(Sepal.Length ~ . - Sepal.Width - Petal.Width, iris) # Exclude predictors from lm() summary(model_b) # Display summary of model # Call: # lm(formula = Sepal.Length ~ . - Sepal.Width - Petal.Width, data = iris) # # Residuals: # Min 1Q Median 3Q Max # -0.75310 -0.23142 -0.00081 0.23085 1.03100 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 3.68353 0.10610 34.719 < 2e-16 *** # Petal.Length 0.90456 0.06479 13.962 < 2e-16 *** # Speciesversicolor -1.60097 0.19347 -8.275 7.37e-14 *** # Speciesvirginica -2.11767 0.27346 -7.744 1.48e-12 *** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.338 on 146 degrees of freedom # Multiple R-squared: 0.8367, Adjusted R-squared: 0.8334 # F-statistic: 249.4 on 3 and 146 DF, p-value: < 2.2e-16
Further Resources
In addition, you might have a look at the related tutorials on this homepage.