2

I'm trying to use Sweave and R to report the results of my statistical analysis. The problem I'm encountering, however, is that I wish to report one-tailed p-values. While it is, of course, simple to know the value of a one-tailed test, I'm having difficulty figuring out how to output a one-tailed value to a table generated automatically.

Basically, I want to create a document that has the code for generating my tables in it, but have the tables report a one-tailed p-value. I'm not even sure if this is possible, but if it is any information would be helpful.

Edit:

Sorry for not having code to start with. I thought there might be a more general answer available, but have some general code below. I'm just using a basic OLS regression with a package called apsrtable to generate the tables.

\documentclass[12pt, letterpaper]{article}


\usepackage{multirow}
\usepackage{dcolumn}


\begin{document} 


<<results=hide,echo=false>>=
library("apsrtable")
x1 = c(100, 123, 1300, 1230, 5453, 4095, 403958, 309458, 2034, 12430)
x2 = c(1000, 2309184, 203948, 240983, 19874, 198479, 918374, 98374198, 8273498, 092834)
y = c(5,10,15,20,25,30,35,40,45,50)
results <- lm(y ~ x1)
results1 <- lm(y ~ x2)
@

\begin{table}
\begin{center}
<<results=tex,echo=false>>=
apsrtable(results, results1, se="pval", stars="default", model.counter=0, order="rl", Sweave=TRUE)
@
\end{center}
\end{table}


\end{document}

So, I'm basically wanting to be able to pass one-tailed p values to apsrtable to make the tables.

As an addition, I'm fairly new to R in general and extremely new to R programming so things like manipulating data frames and such are somewhat difficult for me to understand.

4
  • 1
    What kind of statistical analysis? Specifically, what function are you using to generate the p-values? ETA: Perhaps you could add the code you're currently using? Mar 10, 2012 at 16:06
  • generate the table, use str to figure out its structure, and divide the p-values by 2?
    – Ben Bolker
    Mar 10, 2012 at 16:11
  • Code and further explanation added. Mar 10, 2012 at 21:32
  • It looks like you (or someone else) will probably have to hack apsrtable to get this to work. Not impossible, but a little bit tedious.
    – Ben Bolker
    Mar 10, 2012 at 23:52

1 Answer 1

5

Without knowing which analyses you want to do or some reproducible codes it is hard to give an extensive answer. But here are some tips:

  1. I wrote a small package, swst, aimed at printing statistical results in Sweave. For a small tutorial see my blog, which also contains codes you can use to manually extract p values from htest objects (which a lot of statistical tests in R use). You can find codes to extract p values from some more objects in the source codes. If your object is not yet supported please let me know. Currently I don't think I have implemented options to print one-tailed p-values, but you could look at the source codes to see how you can extract two-tailed p-values and simply divide it by two.

  2. You can use the xtable package to generate LaTeX codes of tables based on a data frame. You can find a very brief introduction to xtable in some slides I made on Sweave (also contains swst package) and by Googling.

Edit:

As for the edited question. It is probably best to manually extract the values and construct the table. Here is an example to report one-tailed t values based on your codes (I changed the data to get some more interesting results).

x1 = rnorm(10)
x2 = rnorm(10)
y = 0.5*x1 + 2*x2
results <- lm(y ~ x1)
results1 <- lm(y ~ x2)

sumRes <- summary(results)$coefficients
sumRes1 <- summary(results1)$coefficients

tab <- data.frame(
  Estimate = paste(round(c(sumRes[2,1],sumRes1[2,1]),3)," (",round(c(sumRes[2,2],sumRes1[2,2]),3),")",sep=""),

  tvalue = round(c(sumRes[2,3],sumRes1[2,3]),3),
  pvalue = c(ifelse(sumRes[2,4]/2<0.001,'$< 0.001$',
   round(sumRes[2,4]/2,3)),ifelse(sumRes1[2,4]/2<0.001,'$< 0.001$',
   round(sumRes[2,4]/2,3)))
  )
names(tab) <- c("Estimate", "$t$-value", "$p$-value")
rownames(tab) <- c("x1","x2")

library("xtable")
print(xtable(tab),sanitize.text.function=function(x)x)
4
  • Bravo, Sacha. Wonderful of you to offer this package and the tutorial.
    – IRTFM
    Mar 10, 2012 at 19:26
  • Thank you, Sacha. The swst package does seem very helpful, but I'm not sure if it will be able to directly address the issue I'm having. I've added some code and further explanation to my original post so that will hopefully clear some things up. Mar 10, 2012 at 21:33
  • Sacha, cool! I'm guessing that your package works fine with knitr (which is like Sweave, but has a lot more features)? I'm guessing it works fine with knitr because using your package relies on \Sexpr so the work is done in R. And since knitr supports \Sexpr it should also work with your package. Does that make sense?
    – Xu Wang
    Mar 11, 2012 at 8:27
  • Thanks! I edited the answer. The package should work with knitr too. I'm haven't tested it yet but I see no reason why it shouldn't. Mar 11, 2012 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.