简体   繁体   中英

R, two way repeated measures ANOVA, how to fix data frame errors? I am stuck on how to proceed

I want to do a two way repeated measures ANOVA, and I am new to R. My data does satisfy the conditions for the procedure and I am interested in doing if applicable a post hoc Tukey (I could do the two way repeated measures ANOVA effortlessly on excel but do not know how to to do it on R and cannot do Tukey on excel)

I have no idea what is wrong with my table as it is shown correctly formatted in the edit but posting is not.

Here is the data:

ID score treatment day
1 15 1 1
2 90 1 1
3 77 1 1
4 23 2 1
5 150 2 1
6 95 2 1
7 48 3 1
8 37 3 1
9 40 3 1
10 190 4 1
11 45 4 1
12 65 4 1
13 23 5 1
14 340 5 1
15 44 5 1
1 34 1 2
2 190 1 2
3 80 1 2
4 17 2 2
5 55 2 2
6 56 2 2
7 33 3 2
8 7 3 2
9 76 3 2
10 3 4 2
11 8 4 2
12 500 4 2
13 78 5 2
14 90 5 2
15 88 5 2

first half of table since it is not formatting correctly

second half of table

I have tried the following codes:

library(readr)
data <- read.csv(file = "sampleData.csv", header = TRUE)  #I have also tried without header = TRUE
data$treatment = as.factor(data$treatment) #treatment is independent categorical variable
data$day = as.factor(data$day) #day is independent categorical variable
data$ID = as.factor(data$ID) #so ID is not treated as a variable
attach(data)
twoWayA <- aov(ID ~ treatment*day + Error(score/(treatment*day)))
summary(twoWayA)

that gives me the following error:

Error in $<-.data.frame ( *tmp* , ID, value = integer(0)): replacement has 0 rows, data has 64

My original data did not have the ID column and used this:

library(readr)
data <- read.csv(file = "No ID sampleData.csv", header = TRUE) #I have also tried without header = TRUE

data$treatment = as.factor(data$treatment) #treatment is independent categorical variable
data$day = as.factor(data$day) #day is independent categorical variable
attach(data)
twoWayA <- aov(score ~ treatment*day + Error(score/(treatment*day)))
summary(twoWayA)

this gave me the following error:

Error in eval(predvars, data, env): object 'score' not found

So just to clarify a two-way ANOVA, or really an ANOVA in general, is used to find out if the means of two groups are statistically different from one another.

They usually have some sort of numerical outcome , or dependent variable , and a categorical input or independent variable .

Due to this I highly doubt you should use patient ID as the outcome , as that would not give you any inference on whether the group means are statistically different from one another.

My supposition is that the score is the outcome and that the treatment and day are the inputs.

Due to this I believe that the formula should be as follows

dat <- data.frame(ID = rep(1:15, 2), score = c(rnorm(30, 40, 5)), treatment = rep(rep(1:5, each = 3), 2), day = rep(1:2, each = 15))


dat$treatment <- as.factor(dat$treatment)
dat$day <- as.factor(dat$day)


twoWayA <- aov(score ~ treatment*day + Error(ID/(treatment*day)), data = dat)
summary(twoWayA)

The above threw no errors for me, and it shouldn't for you either as long as your dataset is the same as shown in the original question.

I think you should use treatment as fixed effect in your equation. twoWayA <- aov(ID ~ treatment*day + Error(ID/(day)). Treatment is fixed because each patients received either treatment 1 or treatment 2 (ect..) but not both. To me you can't add the interaction term in the error because you have one between subject factor which is treatment and one within subject factor which is time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM