简体   繁体   中英

R, GLM model: Selection of significant variables from full model

I need to run a GLM and have tried to select the significant variables with this approach. However, I keep getting an error message.

The input is:

global.model2<-lm(Percent_Mite._rel_abundc ~ Heightc + logNutrientsc + logNDSc + logNNNc + logOxygenc + Patchc + Precipitationc)

Then I run

Select <- summary(global.model2)$coeff < 0.05

resulting in

                 Estimate Std. Error t value Pr(>|t|)
(Intercept)        TRUE      FALSE    TRUE    FALSE
Heightc            TRUE      FALSE    TRUE    FALSE
logNutrientsc     FALSE      FALSE   FALSE    FALSE
logNDSc            TRUE      FALSE    TRUE     TRUE
logNNNc           FALSE      FALSE   FALSE     TRUE
logOxygenc         TRUE      FALSE    TRUE    FALSE
Patchc            FALSE      FALSE   FALSE    FALSE
Precipitationc     TRUE      FALSE    TRUE    FALSE

Next:

Relevant <- names(Select)[Select == TRUE]

Here, the result is

NULL

and the following command

sig.formula <- as.formula(paste("Percent_Mite._rel_abundc ~",paste(Relevant, collapse= "+")))

results in the error message

"Error in parse(text = x, keep.source = FALSE) : <text>:2:0: unexpected end of input 1: Percent_Mite._rel_abundc ~ ^

What am I doing wrong? Some of the variables should be significant.

You are operating on a matrix but the code is assuming a vector. I believe that you want to produce the Boolean vector and not return the entire table of coefficients.

( Select <- summary(global.model2)$coeff[-1,4] < 0.05 )  
( Relevant <- names(Select)[Select == TRUE] )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM