0

I have created a survey for a project. Survey participants responded using a Likert Scale 0-10. This is a before and after scenario. Data was cleaned to remove incomplete responses. I have 1135 paired responses remaining. Data shows numerical. I used the t.test(Q15_2~Q15_3, paired = TRUE) to run my test.

Error in t.test.formula(Q15_2 ~ Q15_3, paired = T) : grouping factor must have exactly 2 levels

Again...Data has been reviewed a dozen times. There are equal before and after responses. This does need to be a paired t-test so I am purposefully using the ~. I can get it to work just fine in excel, but I always like to double check my answers by running them in Excel and R to be sure they are correct. Any input would be welcome!

  • 2
    Welcome to SO, Linda! Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `dput(head(x))` or `data.frame(x=...,y=...)`), and intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans May 04 '20 at 16:05
  • 1
    `(Q15_2 ~ Q15_3` is the formula form. This means Q15_2 group by Q15-3. If you are looking for a comparison between Q15_2 and Q15_3, then you need to use a ", " thus `(Q15_2, Q15_3` – Dave2e May 04 '20 at 16:09
  • 1
    You can't do a paired t.test using the formula method. – Edward May 04 '20 at 16:10
  • I did use the "," as paired, but the result I received is completely different than the one I received in Excel using the t.test or the ttest on the columns. As a matter of fact, they aren't even close. In Excel I had both columns, two-sided and paired. How am I coming up with such different answers if "," is the correct method? – Linda Feshami May 04 '20 at 16:15
  • Have you considered that Excel could be wrong? – Edward May 04 '20 at 16:19
  • I have...which is why I am trying to figure out a third way to confirm which one is right ;) – Linda Feshami May 04 '20 at 16:21
  • My SPSS was on an old computer ;) – Linda Feshami May 04 '20 at 16:22
  • Another cause might be the way you are importing the data into R. This has been the cause of many problems in the past here. I suggest you show some code in your question, together with some data. – Edward May 04 '20 at 16:22
  • I'm pretty sure I am importing it correctly and verified that it is being read as numbers...but I'm here to learn :) – Linda Feshami May 04 '20 at 16:24

1 Answers1

1

I think you should use:

t.test(Q15_2, Q15_3, paired = TRUE) 

with a comma not a tilde. This comma syntax is applied if both vectors contain pairs of data in their columns. The formula syntax with ~ is applied if the left contains the data of both sets while the right vector is a factor with the corresponding two treatments.

Example:

x1 <- c(4.71, 6.66, 5.01, 4.6, 3.98)
x2 <- c(5.42, 7.1, 5.52, 6.05, 6.23)
t.test(x1, x2, paired=TRUE)

    Paired t-test

data:  x1 and x2
t = -3.1096, df = 4, p-value = 0.03589
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
 -2.029145 -0.114855
sample estimates:
mean of the differences 
                 -1.072 

And in Excel with the same data (values copied, labels translated from local language to English):

t-Statistics                       -3.109611652
P(T<=t) one sided                   0.017942691
critical t-value (one sided t-Test) 2.131846786
P(T<=t) two-sided                   0.035885381
critical t-value (two sided t-Test) 2.776445105

We see that the p-values for the two-sided hypothesis are the same.

tpetzoldt
  • 5,338
  • 2
  • 12
  • 29
  • I agree that the comma vs the tilde is correct. My problem is R-Studio is giving me one answer and both SPSS and Excel agree on another number. I've never had this happen before without being able to figure out "why" and get them to match...the fact that SPSS is agreeing with Excel makes me think my problem is R Studio as I'm not very experienced with it. I needed to perform the operation two different ways and chose Excel and R-Studio first... I finally loaded it into SPSS. I'm sure I'm missing something simple, but I would love to understand what I'm missing for the next time. – Linda Feshami May 04 '20 at 23:28
  • 1
    @LindaFeshami How can anyone help if you don't provide some part of the data and the code to import the data and run the t.test? Read the very first comment above and do what was asked. – Edward May 04 '20 at 23:39