简体   繁体   English

R,双向重复测量方差分析,如何修复数据帧错误? 我被困在如何继续

[英]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.我想做一个双向重复测量方差分析,我是 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)我的数据确实满足程序的条件,如果适用,我有兴趣做事后 Tukey(我可以在 excel 上毫不费力地做两种重复测量 ANOVA,但不知道如何在 R 上做 Tukey优秀)

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 ID score分数 treatment治疗 day
1 1 15 15 1 1 1 1
2 2 90 90 1 1 1 1
3 3 77 77 1 1 1 1
4 4 23 23 2 2 1 1
5 5 150 150 2 2 1 1
6 6 95 95 2 2 1 1
7 7 48 48 3 3 1 1
8 8 37 37 3 3 1 1
9 9 40 40 3 3 1 1
10 10 190 190 4 4 1 1
11 11 45 45 4 4 1 1
12 12 65 65 4 4 1 1
13 13 23 23 5 5 1 1
14 14 340 340 5 5 1 1
15 15 44 44 5 5 1 1
1 1 34 34 1 1 2 2
2 2 190 190 1 1 2 2
3 3 80 80 1 1 2 2
4 4 17 17 2 2 2 2
5 5 55 55 2 2 2 2
6 6 56 56 2 2 2 2
7 7 33 33 3 3 2 2
8 8 7 7 3 3 2 2
9 9 76 76 3 3 2 2
10 10 3 3 4 4 2 2
11 11 8 8 4 4 2 2
12 12 500 500 4 4 2 2
13 13 78 78 5 5 2 2
14 14 90 90 5 5 2 2
15 15 88 88 5 5 2 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 $<-.data.frame ( *tmp* , ID, value = integer(0)) 中的错误:替换有 0 行,数据有 64

My original data did not have the ID column and used this:我的原始数据没有 ID 列并使用了这个:

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 eval 中的错误(predvars、data、env):未找到 object 'score'

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.因此,只是为了澄清双向 ANOVA,或者实际上是一般的 ANOVA,用于确定两组的平均值是否在统计上彼此不同。

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.因此,我非常怀疑您是否应该使用患者 ID 作为结果,因为这不会给您任何关于组均值是否在统计上彼此不同的推断。

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. twoWayA <- aov(ID ~ treatment*day + Error(ID/(day))。治疗是固定的,因为每个患者都接受了治疗 1 或治疗 2(等等),但不能同时接受。对我来说,你不能添加错误中的交互项,因为您在主题因素(即治疗)和主题因素(即时间)之间有一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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