1

Calling

coeftest(m)

returns z test of coefficients:

         Estimate Std. Error  z value  Pr(>|z|)    
lenDelta  0.26592    0.13038   2.0397   0.04138 *  
-2|-1    -2.59586    0.24090 -10.7758 < 2.2e-16 ***
-1|0     -0.81155    0.13558  -5.9860 2.150e-09 ***
0|1       0.73271    0.13394   5.4706 4.486e-08 ***
1|2       1.98097    0.19182  10.3271 < 2.2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

I know I can access the individual numerical values with

coeftest(m)[1,4]

From this I can get the significance code in a straightforward way.

However, how do I access the significance code string "*" associated with lenDelta?

The structure of the object is as follows

str(coeftest(m))

returns the following output

 coeftest [1:5, 1:4] 0.266 -2.596 -0.812 0.733 1.981 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:5] "lenDelta" "-2|-1" "-1|0" "0|1" ...
  ..$ : chr [1:4] "Estimate" "Std. Error" "z value" "Pr(>|z|)"
 - attr(*, "method")= chr "z test of coefficients"

Complete reproducible example (sourced from here)

## data 
library("foreign")
dat <- read.dta("ats.ucla.edu/stat/data/ologit.dta")
## model 
library("MASS")
m <- polr(apply ~ pared + public + gpa, data = dat, Hess = TRUE)
## coefficient test
library("AER")
coeftest(m)

Thanks

tomas
  • 355
  • 4
  • 9

2 Answers2

2

The print method for coeftest objects internally calls printCoefmat (just like the summary method for lm or glm objects). And this leverages the function symnum, by default using the cutpoints shown in the legend of coefficient tests/summaries.

To call this by hand you can do the following. (I'm using a simpler model here because I had problems accessing the data at the UCLA site.)

Fit a model:

m <- lm(dist ~ speed, data = cars)

Extract the p-values from coeftest:

pv <- coeftest(m)[,4]

Cut the p-values into significance groups:

symnum(pv, corr = FALSE, na = FALSE,
  cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
  symbols = c("***", "**", "*", ".", " ")
)
## (Intercept)       speed 
##           *         *** 
## attr(,"legend")
## [1] 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Achim Zeileis
  • 15,710
  • 1
  • 39
  • 49
  • Actually, my question was more in the direction of how would I parse or get the significance code from the object returned by coeftest, rather then to compute them from the p-values myself. Essentially, the object somewhere contains the "Signif. codes:" line, I am just unable to access it. Put in another way, I do not understand the structure of the object as returned by the str() call. What does .$ : chr mean? How is it accessed? – tomas Feb 12 '16 at 16:17
  • The line starting with `Signif. codes` is _not_ part of the `coeftest` object - the latter is simply a numeric matrix with suitable labels. The `Signif. codes` legend is produced on-the-fly by the `print` method (as I tried to convey in my answer above) using the function `symnum()`. This also sets up the legend. – Achim Zeileis Feb 12 '16 at 22:34
  • I am having the same problem. Is there no way to convert the object to a matrix or data frame that can be easier accessed? – Raphael Oct 03 '16 at 18:42
  • You need to be more specific: What exactly do you want to access easily? The `coeftest()` _is_ a matrix where the 4th column pertains to the p-values. It's just that the significance stars are not part of that matrix. To obtain groups of significances you can simply `cut()` the p-values or use `symnum()` as shown in my answer. If you want something different, please provide more details...possibly in a separate question. – Achim Zeileis Oct 03 '16 at 19:11
0

I suggest using the below function ctdf to convert the coeftest print output to a data frame that can be easily accessed with conventional methods such as r2$sig . To specifically access the significance stars of "lenDelta" use r2[row.names(r2)=="lenDelta","sig"]

# Obtain coeftest object
r1=coeftest(m)                         

#*** Function to convert coeftest results object to data frame
ctdf=function(x){
  rt=list()                             # generate empty results list
  for(c in 1:dim(x)[2]) rt[[c]]=x[,c]   # writes column values of x to list
  rt=as.data.frame(rt)                  # converts list to data frame object
  names(rt)=names(x[1,])                # assign correct column names
  rt[,"sig"]=symnum(rt$`Pr(>|z|)`, corr = FALSE, na = FALSE,cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),symbols = c("***", "**", "*", ".", " "))
  return(rt)
}

# Apply function
r2=ctdf(r1) # returns a data frame that can be accessed normally
Raphael
  • 1,143
  • 1
  • 11
  • 16