0

I am trying to run a paired t-test in R on data grouped by factors:

> head(i.o.diff,n=20)
#   Difference Tree.ID Tree.Name   Ins Outs
#1        0.20    AK-1      Akun  1.20  1.0
#2       -1.60    AK-2      Akun  0.40  2.0
#3       -0.60    AK-3      Akun  1.40  2.0
#4        0.40    AK-4      Akun  0.40  0.0
#5        1.30    AK-5      Akun  1.80  0.5
#6        2.70     J-1     Jaror 10.20  7.5
#7        6.60     J-2     Jaror 10.60  4.0
#8        2.50     J-3     Jaror  6.00  3.5
#9        7.50     J-4     Jaror 22.00 14.5
#10      -4.50     J-5     Jaror  5.00  9.5
#11       3.50    Ce-1     Ku'ch  4.00  0.5
#12      -0.70    Ce-2     Ku'ch  4.80  5.5
#13       1.60    Ce-3     Ku'ch  2.60  1.0
#14      -2.40    Ce-4     Ku'ch  2.60  5.0
#15      -1.75    Ce-5     Ku'ch  2.25  4.0

I first tried using:

pairwise.t.test(i.o.diff$In,i.o.diff$Out,g=i.o.diff$Tree.Name,paired=TRUE,pool=FALSE,p.adj="none",alternative=c("less"),mu=0)

but I get the error

Error in complete.cases(x, y) : not all arguments have the same length

which doesn't make a whole lot of sense to me.

I considered using ddply(), apply(), and summaryBy(), but couldn't get it to work because the inputs for the paired t-test require 2 vectors and most of the previous functions I mention seem to work best when only one column is being "operated" upon.

In order to get around this, I tried to use a for loop to achieve the same end:

for(i in unique(i.o.diff$Tree.Name)) {
  pair_sub<-subset(i.o.diff,Tree.Name==i) 
  t.pair<-t.test(pair_sub$Ins,pair_sub$Outs,paired="True")
  print(t.pair)
}

However when I do this, I get error

in paired || !is.null(y) : invalid 'x' type in x||y

So I checked typeof(pair_sub$Ins). Turns out that type is double, which is numeric, so I am not sure why the paired t-test is not working. Any ideas as to how to fix either of these methods?

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
elduderino260
  • 223
  • 3
  • 12
  • 2
    i.o.diff$In shoud be i.o.diff$Ins, with an 's' at the end... same for i.o.diff$Outs. No? – Roger Mar 15 '15 at 20:41
  • Simple typos are a reason to close if the questioner does not delete. – IRTFM Mar 15 '15 at 21:25
  • I tried reproducing this; even after correcting for typos, I don't get the same errors you get. You might want to restart your R session afresh and try again. – Dominic Comtois Mar 16 '15 at 03:40
  • That is correct Roger, but I just fixed the typos and got the exact same errors. I did restart my R session to no avail... – elduderino260 Mar 16 '15 at 14:12
  • Furthermore, there were no typos in my for loop, so that doesn't answer that issue. – elduderino260 Mar 16 '15 at 14:18
  • I also checked `> length(i.o.diff$Ins)` `[1] 51` `> length(i.o.diff$Outs)` `[1] 51` So I still can't figure why I get that error for pairwise.t.test. – elduderino260 Mar 16 '15 at 14:25

2 Answers2

0

Removed the quotes around TRUE in the for loop. Works great now.

elduderino260
  • 223
  • 3
  • 12
0

From R documentation: t.test {stats} R Documentation Student’s t-Test:

Description:

     Performs one and two sample t-tests on vectors of data. Usage t.test(x, …)

Default S3 method:
t.test(x, y = NULL, alternative = c(“two.sided”, “less”, “greater”), mu = 0, paired = FALSE, var.equal = FALSE, conf.level = 0.95, …)
lrleon
  • 2,610
  • 3
  • 25
  • 38
FTF
  • 181
  • 3
  • 11