0

I'm currently working with data from a questionnaire where answers have been added up to find before and after scores and subsequently subtracted to find differences. I am trying to run a Mann-Witney U test to test if there is a difference between the difference scores after viewing different educational interventions. The data is arranged so one column is the differences from the first educational intervention and the second column is the differences from the second educational intervention. When I run the code:

wilcox.test(formula=opinion$video~opinion$writtenpiece)

I get this error:

Error in model.frame.default(formula = opinion$video ~ opinion$writtenpiece) : invalid type (NULL) for variable 'opinion$video'

I've tried everything I can think of to make it work and continue to get the same error. Does anyone have any ideas what I'm doing wrong? It's been a long time since I've used R for data analysis and I've never done it for this type of data, so I'm sure I'm missing something.

This is what the data looks like (I'm not sure why my numbers have an L next to them...)

dput(opinion)

structure(list(Video = c(11L, 12L, 10L, 10L, 9L, 9L, 13L, 13L, 10L, 12L, 12L, 12L, 13L, 11L, 15L, 11L, 15L, 12L, 17L, 12L, 10L, 14L, 13L, 12L, 12L, 10L, 9L, 11L, 11L, 14L, 13L, 14L, 14L, 11L, 12L, 13L, 12L, 13L, 14L, 11L, 10L, 12L, 13L, 15L, 15L, 10L, 11L, 13L, 12L, 11L, 12L, 9L, 10L, 12L, 14L, 10L, 10L, 9L, 11L, 11L, 10L, 9L, 10L, 10L, 16L, 7L, 9L, 10L, 10L, 16L, 13L, 11L, 10L, 6L, 11L, 10L, 13L, 10L, 13L, 12L, 10L, 15L, 0L, 0L, 0L, 0L, 0L, 0L), Written.Piece = c(10L, 11L, 10L, 10L, 10L, 7L, 10L, 9L, 13L, 13L, 12L, 8L, 13L, 12L, 15L, 10L, 9L, 11L, 10L, 11L, 13L, 10L, 12L, 11L, 11L, 11L, 10L, 15L, 10L, 13L, 14L, 11L, 11L, 12L, 9L, 15L, 11L, 14L, 11L, 12L, 12L, 14L, 10L, 10L, 10L, 9L, 13L, 13L, 10L, 9L, 9L, 13L, 8L, 13L, 14L, 9L, 12L, 11L, 11L, 12L, 10L, 13L, 16L, 12L, 10L, 8L, 13L, 16L, 17L, 12L, 11L, 13L, 11L, 11L, 9L, 10L, 12L, 12L, 10L, 14L, 12L, 11L, 12L, 11L, 12L, 10L, 10L, 12L)), .Names = c("Video", "Written.Piece"), class = "data.frame", row.names = c(NA, -88L))

2
  • 2
    R is telling you that the variable doesn't exist. Commented Aug 11, 2017 at 16:35
  • Roland how could one exist and one not? As it always says it's for the video one. The video has fewer scores than the written piece but I even tried putting in code to ignore NAs. Commented Aug 11, 2017 at 16:39

1 Answer 1

0

It looks like the command you're using to run your wilcox.test isn't correctly referencing your data. Specifically, from your dput it appears that the Video and Written.Piece variables are capitalized. Try running this code:

melt.opinion <- melt(opinion, 
                       id.vars = c(), 
                       measure.vars = c("Video", "Written.Piece"), 
                       variable.name = "intervention.type", 
                       value.name = "difference.value")
wilcox.test(formula = 
              melt.opinion$difference.value ~ melt.opinion$intervention.type) 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! I had just noticed this myself and fixed it, but then I get this error: Error in wilcox.test.formula(formula = opinion$Video ~ opinion$Written, : grouping factor must have exactly 2 levels I added all the scores to one column and added a second column called intervention with 1s for the video scores and 2s for the written scores. I finally got an output, but I'm not sure that's an accurate use of that test or if it's truly testing what I want it to?
That's not an accurate test as I did it with the empathy scores as well and got the exact same p-value so I did a Kruskal Wallis test instead. Is that an accurate test?
My statistics is a little shaky, but on reading some of the wikipedia pages, it seems like the wilcox test should work since your sample is only looking at two levels. If I had to guess (and if wilcox.test works anything like R's lm, glm, etc), you should reformat your data using a melt command. I'm going to edit my response/answer for formatting reasons.
Thank you for helping me! I'm afraid I tried the melt function you suggested and got the same error about "NULL" from the original post I made. I'm assuming thats due to another mistake I've made but I'm afraid I can't see it. What I can't figure out is why it would run the Kruskal Wallis test and not the Mann-Witney when it's only two levels.
I just tried using the code that I sent you and it ran the Mann-Witney test fine. Is your software up to date? You might have to get ride of the "nice" formatting, so try: melt.opinion <- reshape2::melt(opinion,id.vars=c(),measure.vars=c("Video", "Written.Piece"),variable.name="intervention.type",value.name="difference.value") wilcox.test(formula=melt.opinion$difference.value~melt.opinion$intervention.type)
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.