Kirsten
Kirsten

Reputation: 18130

How to sort the output of summary by a column?

I want to sort the summary output by p-value I tried

lm.fit <- lm(TARGET ~ .,train)
df<-summary(lm.fit)

colnames(df,c("Predictor","Estimate","StdError","tvalue","pvalue","signifcodes"))
rev(sort(lm.fit$pvalue)) -> cf

but I get an error

Warning message in if (do.NULL) NULL else if (nc > 0L) paste0(prefix, seq_len(nc)) else character(): "the condition has length > 1 and only the first element will be used" Error in if (do.NULL) NULL else if (nc 0L) paste0(prefix, seq_len(nc)) else character(): argument is not interpretable as logical Traceback:

  1. colnames(df, c("Predictor", "Estimate", "StdError", "tvalue", . "pvalue", "signifcodes"))

Upvotes: 2

Views: 1383

Answers (1)

Ben
Ben

Reputation: 30494

The df object obtained from summary is not a data frame or matrix, and the following is needed for colnames:

a matrix-like R object, with at least two dimensions

You could extract coefficients and then determine order and sort. Here is one way to approach this:

lm.fit <- lm(mpg ~ ., mtcars)

smry.lm <- summary(lm.fit)
coef.lm <- coef(smry.lm)

i <- order(coef.lm[,4], decreasing = TRUE)
coef.lm[i, ]

With broom, you could do the following as well:

library(broom)

coef.tidy <- tidy(lm.fit)
colnames(coef.tidy) <- c("Predictor", "Estimate", "StdError", "tvalue", "pvalue")
coef.tidy[order(coef.tidy$pvalue, decreasing = TRUE), ]

Output

   Predictor   Estimate StdError tvalue pvalue
   <chr>          <dbl>    <dbl>  <dbl>  <dbl>
 1 cyl          -0.111    1.05   -0.107 0.916 
 2 vs            0.318    2.10    0.151 0.881 
 3 carb         -0.199    0.829  -0.241 0.812 
 4 gear          0.655    1.49    0.439 0.665 
 5 drat          0.787    1.64    0.481 0.635 
 6 (Intercept)  12.3     18.7     0.657 0.518 
 7 disp          0.0133   0.0179  0.747 0.463 
 8 hp           -0.0215   0.0218 -0.987 0.335 
 9 qsec          0.821    0.731   1.12  0.274 
10 am            2.52     2.06    1.23  0.234 
11 wt           -3.72     1.89   -1.96  0.0633

Upvotes: 2

Related Questions