0

I have a data which I am working on it. I need to run repeated measures Anova test on it but first I have to reshape data to long format. I did something as shown on website, reshaping doesn't give any error but I don't think it works. So Anova test gives error. Here is my code and error.

# reshaping to long format
id=1:length(veri$SIRA)
k.1 <- veri$KOLEST
k.2 <- veri$KOLEST2
k.3 <- veri$KOLEST3
veri2 <- data.frame(id,k.1,k.2,k.3)
longformat <- reshape(veri2,direction="long", varying=list("k.1","k.2","k.3"), idvar="id")

This is output for longformat

  id  time k.1 k.2 k.3
1  1    1 209 195 181
2  2    1 243 184 172
3  3    1 192 178 162
4  4    1 210 112  93
5  5    1 190 188 172
6  6    1 232 169 156

Time is 1 all along. This seems little odd to me. I thought it shoud be 1-2-3 according to 3 different measures.

And this is error when I run the test:

repmesao <- aov(k~time+Error(id/time), data=longformat)

Error in model.frame.default(formula = k ~ id/time, data = longformat, :
invalid type (list) for variable 'k'

How I can fix this problem? Any suggestions?

989
  • 12,579
  • 5
  • 31
  • 53
  • You should specify your `reshape` function as follows: `reshape(veri2, direction = 'long', varying = list(names(veri2)[3:5]), idvar=c('id','time'), timevar = 'k', v.names = 'value')` – Jaap Jun 03 '16 at 19:21

1 Answers1

0

For reshaping, use library(tidyr) and a command like so

data_long <- gather(data, group, dv, range of columns)  

If you have more than on dv, then this procedure is not good. What I usually do is divide data into dvs like data_dv1 <- data[1:3] and data_dv2 <- cbind(data[1:2], data[4]). I reshape it as shown above, then I just cbind(data_dv1_long, data_dv2_long) minding that not all columns should be combined, as you will have for instance your subject id in both df, so choose the columns for cbind accordingly.

Also, don't know that you are going to use for ANOVA, but I recommend library(ez) with a command like so

ezANOVA(data=data, dv=.(dv), wid=.(subject_id), within=.(group1), between=.(group2), detail=T)
mike
  • 173
  • 11