我在尝试创建固定效果(模型1)双向ANOVA数据的交互图时遇到编码问题。我输入并将我的数据从excel导入RStudio。数据如下。
Sex Genotype Activity
Female I 2.838
Female I 4.216
Female I 2.889
Female I 4.198
Female II 3.55
Female II 4.556
Female II 3.087
Female II 1.943
Female III 3.62
Female III 3.079
Female III 3.586
Female III 1.943
Male I 1.884
Male I 2.283
Male I 2.939
Male I 1.486
Male II 2.396
Male II 2.956
Male II 3.105
Male II 2.649
Male III 2.801
Male III 3.421
Male III 2.275
Male III 2.11
然后我将其保存为Excel工作簿。然后我在RStudio中找到文件>导入数据集> Excel>选择我的数据集文件>选择第一行作为名称&打开数据查看器。 我检查了我的数据,它全部在三列的三个标题下。 然后做我的ANOVA我做了:
Model_1 <- aov(Activity ~ Sex*Genotype, data=data7)
> summary(Model_1)
Df Sum Sq Mean Sq F value Pr(>F)
Sex 1 3.527 3.527 6.563 0.0196 *
Genotype 2 0.178 0.089 0.165 0.8488
Sex:Genotype 2 1.166 0.583 1.085 0.3591
Residuals 18 9.673 0.537
接下来,我尝试制作一个互动情节:
interaction.plot(Genotype,Sex,Activity, fun = mean, type= c("b"), xlab=
"Genotype" ,ylab = "Enzyme Activity (enzyme unit (U)=1µmol min-
1)",main="Interaction Plot" )
但是,当我这样做时,我收到了这个错误:
Error in tapply(response, list(x.factor, trace.factor), fun) : object 'Genotype' not found
如何将基因型和性别作为对象?他们不应该已经成为ANOVA表中显示的对象吗?
另外,我想为每个单元格制作一个Mean,SD和n的表,但是当我尝试使用单因素方差分析时,它不适用于双向。你怎么做这样的桌子?
我在网上尝试了以下示例,但是没有一个能帮助我创建交互图,我还没有看到任何关于如何制作表格的好例子(即使在这个网站上), SD和n用于双向ANOVA,适用于我的情况。 我有什么方法可以用来帮助我吗?
如果有人能帮助解释我做错了什么以及如何制作表格,我会非常感激。
答案 0 :(得分:1)
两种方法。 使用基本功能
#nj is your dataframe which I loaded using
nj <-read.table("nj_data.txt", header = T)
#Base R - using built-in functions
# the Dot indicates variable(column) to work on
# if you have more than one variable use their names
#good practice to use na.rm = T, to make sure you exclude
# NA - missing values, other wise mean and sd will report NA
aggregate(. ~ Sex+Genotype, data = nj,
FUN = function(x) c(Mean = mean(x, na.rm = T),
n = length(x),
sd = sd(x, na.rm = T)))
#using dplyr - I suggest this as syntax is nicer
library(dplyr)
nj %>% group_by(Sex, Genotype) %>%
summarise_all(funs(mean(., na.rm = T),sd(., na.rm = T),n()))
#here the "." in mean and sd means using the data given
#equivalent to x in base R above
读取Tidyverse(一组新的库,使数据分析更容易) 在https://www.tidyverse.org/
以及ggplot - 这是使用ggplot进行交互图的一个很好的入门 https://sebastiansauer.github.io/vis_interaction_effects/
答案 1 :(得分:0)
这是最基本的解决方案,也许很有效但是很有效:
data7$group=paste(data7$Sex,data7$Genotype,sep="_")
tapply(data7$Activity,data7$group,sd)