Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting output from statistical tests

Tags:

output

r

I run a statistical test in R (running in RStudio). I save the result using a variable name. I want to extract one part of the result. How do I do this? Here is an example with the R code at the end. I set up an experiment with four treatments, and gather data. I next run ANOVA and perform a Tukey HSD test. The result is stored in a variable called "posthoc."

I look and note that posthoc is a list of 1. In RStudio I see a little blue arrow to the left of the name, and clicking on that gives more information. I am not sure how to interpret it in a way that I can use to answer my own question.

I can print(posthoc) and I get the following.

#  Tukey multiple comparisons of means
#    95% family-wise confidence level
#
#Fit: aov(formula = Expt1$Treat1 ~ Expt1$Trt)
#
#$`Expt1$Trt`
#          diff        lwr          upr     p adj
#B-A   6.523841   2.664755  10.38292569 0.0001372
#C-A  18.584160  14.725075  22.44324507 0.0000000
#D-A   2.643719  -1.215367   6.50280370 0.2854076
#C-B  12.060319   8.201234  15.91940456 0.0000000
#D-B  -3.880122  -7.739207  -0.02103681 0.0482260
#D-C -15.940441 -19.799527 -12.08135619 0.0000000

I can also type class(posthoc) and I get this: [1] "TukeyHSD" "multicomp"

In this case, what I need are all the p-values in a new variable. The general problem is that R gives me output and I need to be able to figure out how to extract specific elements of that output. I might be using aov, lm, nlme, or something else.

Mean1=3.2
Sd1=3.2
Mean2=9.4
Sd2=2.4
Mean3=21.4
Sd3=6.4
Mean4=3.9
Sd4=10.7

Size1=30

Treat1=rnorm(Size1,mean=Mean1, sd=Sd1)
Trt="A"
Treat1M <- data.frame(Treat1, Trt)
Treat1=rnorm(Size1,mean=Mean2, sd=Sd2)
Trt="B"
Treat2M <- data.frame(Treat1, Trt)
Treat1=rnorm(Size1,mean=Mean3, sd=Sd3)
Trt="C"
Treat3M <- data.frame(Treat1, Trt)
Treat1=rnorm(Size1,mean=Mean4, sd=Sd4)
Trt="D"
Treat4M <- data.frame(Treat1, Trt)

Expt1=rbind(Treat1M, Treat2M, Treat3M, Treat4M)

Expt1R<-aov(Expt1$Treat1 ~ Expt1$Trt)
posthoc <-TukeyHSD(x=Expt1R, 'Expt1$Trt', conf.level=.95)
like image 555
TimothyEbert Avatar asked Aug 20 '26 05:08

TimothyEbert


2 Answers

simply use subsetting of your posthoc variable.

posthoc$`Expt1$Trt`[,4]

or you can try the broom package.

library(broom)
res <- tidy(posthoc)
res
       term comparison   estimate    conf.low conf.high  adj.p.value
1 Expt1$Trt        B-A   5.904138   1.3639293 10.444346 5.223255e-03
2 Expt1$Trt        C-A  16.886340  12.3461316 21.426548 3.919087e-14
3 Expt1$Trt        D-A   4.283597  -0.2566111  8.823805 7.189220e-02
4 Expt1$Trt        C-B  10.982202   6.4419940 15.522410 3.226398e-08
5 Expt1$Trt        D-B  -1.620540  -6.1607487  2.919668 7.886097e-01
6 Expt1$Trt        D-C -12.602743 -17.1429509 -8.062534 3.235351e-10

The output of the tidy function is a data.frame. Thus, you can access the p-values using res$adj.p.value.

class(res)
[1] "data.frame"
like image 175
Roman Avatar answered Aug 22 '26 06:08

Roman


Jimbou has already provided an excellent solution. I would also go for broom especially when doing plots with ggplot2. I would just like to expound on the comment by John Coleman.

You can inspect the object by using str. In case of posthoc,

str(posthoc)

gives

List of 1
 $ Expt1$Trt: num [1:6, 1:4] 6.46 18.19 -0.76 11.74 -7.22 ...

Typing

posthoc$`Expt1$Trt`

gives

     diff     lwr     upr     p adj
B-A   6.4562   2.130  10.782 9.530e-04
C-A  18.1922  13.866  22.519 2.454e-14
D-A  -0.7598  -5.086   3.566 9.680e-01
C-B  11.7360   7.410  16.062 7.427e-10
D-B  -7.2160 -11.542  -2.890 1.725e-04
D-C -18.9521 -23.278 -14.626 1.588e-14

So you can access the fourth column by typing

posthoc$`Expt1$Trt`[,4]

or

posthoc$`Expt1$Trt`[,'p adj']

For some objects like Expt1R, the output of str() can sometimes be overwhelming. Using names() to look at the different objects inside it is also helpful.

names(Expt1R)

 [1] "coefficients"  "residuals"     "effects"       "rank"          "fitted.values" "assign"       
 [7] "qr"            "df.residual"   "contrasts"     "xlevels"       "call"          "terms"        
[13] "model" 

So

Expt1R$df.residual

will give you the degrees of freedom of the residual.

like image 31
hpesoj626 Avatar answered Aug 22 '26 05:08

hpesoj626