[R] Displaying p-values in latex

Frank E Harrell Jr f.harrell at vanderbilt.edu
Fri Jul 29 05:38:40 CEST 2005


Marc Schwartz wrote:
> On Thu, 2005-07-28 at 14:00 -0700, Juned Siddique wrote:
> 
>>Hi. I want to create a table in latex with regression coefficients and their
>>corresponding p-values. My code is below. How do I format the p-values so
>>that values less than 0.0001 are formated as <.0001 rather than just rounded
>>to 0.0000? Thank you.
>>
>>model<-lm(y~x1+x2)
>>
>>output<-summary(model)
>>output<-as.matrix(coefficients(output))
>>output<-format.df(ouput,cdec=c(2,2,2,4))
>>
>>latex(output,longtable=TRUE, file="C:/model.tex")
> 
> 
> I have not used Frank's latex() function, so there may be a setting that
> helps with this, but something along the lines of:
> 
> # Using one example from ?lm:
> 
> ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
> trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
> group <- gl(2,10,20, labels=c("Ctl","Trt"))
> weight <- c(ctl, trt)
> lm.D9 <- lm(weight ~ group)
> 
> output <- (summary(lm.D9))
> output <- as.matrix(coefficients(output))
> output <- format.df(output, rdec = c(2, 2, 2, 4))
> 
> # Here is the line to re-format the p values
> # Note that column alignment on the decimal may be problematic here 
> # depending upon the specifications for column justifications. 
> # If the column is right justified, it should be easier, since we 
> # are forcing four decimal places.
> 
> output[, 4] <- ifelse(as.numeric(output[, 4]) < 0.0001, 
>                       "$<$0.0001", sprintf("%6.4f", output[, 4]))
> 
> 
> 
>>output
> 
>             Estimate Std. Error t value Pr(>|t|)   
> (Intercept) "5.03"   "0.22"     "22.85" "$<$0.0001"
> groupTrt    "-0.37"  "0.3114"   "-1.19" "0.2490"   
> attr(,"col.just")
> [1] "r" "r" "r" "r"
> 
> 
> Note the use of "$" to indicate math mode for the symbols. Presumably
> Frank has a similar approach when the object passed to latex() is a
> model, since the heading for the p value column should end up being
> something like:
> 
>    Pr($>$$|$t$|$)
> 
> and the minus signs in the second line should similarly be surrounded:
> 
>    $-$0.37
> 
> I have cc:d Frank here for his clarifications.
> 
> HTH,
> 
> Marc Schwartz
> 
> 
> 

I've only implemented a fully automatic latex function for anova output, 
which handles P-value printing as requested.  Look at anova.Design for 
the code. -Frank

-- 
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University




More information about the R-help mailing list