Search code examples
rsweave

one-tailed test with Sweave and R


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.


Solution

  • 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)