我目前在R中制作了很多ANOVA,我的代码如下所示: 我的数据如下:
structure(list(Vial_nr = c(151L, 151L, 151L, 162L), Concentration = structure(c(1L,
1L, 1L, 1L), .Label = c("a", "b", "c", "d", "e", "x", "y"), class = "factor"),
Line = structure(c(1L, 1L, 1L, 1L), .Label = c("20", "23",
"40", "73"), class = "factor"), Sex = structure(c(1L, 1L,
1L, 1L), .Label = c("f", "m"), class = "factor"), Fly = structure(c(1L,
2L, 3L, 1L), .Label = c("1", "2", "3"), class = "factor"),
Temp = structure(c(1L, 1L, 1L, 1L), .Label = c("23", "29"
), class = "factor"), X0.5_sec = c(51.84, 41.28, 8.64, 28.8
), X1_sec = c(41.76, 8.64, 10.56, 42.72), X1.5_sec = c(42.72,
17.28, 10.08, 57.12), X2_sec = c(51.36, 29.76, 19.68, 71.52
), X2.5_sec = c(52.8, 44.64, 39.36, 69.12), X3_sec = c(55.68,
52.8, 58.08, 82.56), Vial = structure(c(138L, 138L, 138L,
149L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33", "34", "35", "36", "37", "38",
"40", "41", "42", "43", "47", "49", "50", "56", "57", "59",
"61", "62", "63", "64", "66", "67", "68", "69", "70", "71",
"72", "73", "74", "75", "76", "77", "78", "79", "80", "81",
"82", "83", "84", "85", "86", "87", "88", "89", "90", "91",
"92", "93", "94", "95", "96", "97", "98", "99", "100", "101",
"102", "103", "104", "105", "106", "107", "108", "109", "110",
"111", "112", "113", "114", "115", "116", "117", "118", "119",
"120", "121", "122", "123", "124", "125", "126", "127", "128",
"129", "130", "131", "132", "133", "134", "135", "136", "137",
"138", "139", "140", "141", "142", "143", "144", "145", "146",
"147", "148", "149", "150", "151", "152", "153", "154", "155",
"156", "157", "158", "159", "160", "161", "162", "163", "164",
"165", "166", "167", "168", "169", "170", "171", "172", "173",
"174", "175", "176", "177", "178", "179", "180", "181", "182",
"183", "184", "185", "186", "187", "188", "189", "190", "191",
"192", "193", "194", "195", "196", "197", "198", "199", "200",
"201", "202", "203", "204", "205", "206", "207", "208", "209",
"210", "211", "212", "213", "214", "215", "216", "217", "218",
"219", "220", "221", "222", "223", "224", "225", "226", "227",
"228", "229", "230", "231", "232", "233", "234", "235", "236",
"237", "238", "239", "240", "241", "242", "243", "244", "245",
"246", "247", "248", "249", "250", "251", "252", "253", "254",
"255", "256", "257", "258", "259", "260", "261", "262", "263",
"264", "265", "266", "267", "268", "269", "270", "271", "273",
"274", "275", "276", "277", "278", "279", "280", "461", "462",
"463", "464", "465", "466", "467", "468", "469", "470", "471",
"472", "473", "474", "475", "476", "477", "478", "479", "480",
"481", "482", "483", "484", "485", "486", "487", "488", "489",
"490", "491", "492", "493", "494", "495", "496", "497", "498",
"499", "500", "501", "502", "503", "504", "505", "506", "507",
"508", "509", "510", "511", "512", "513", "514", "515", "516",
"517", "518", "519", "520", "521", "522", "523", "524", "525",
"526", "527", "528", "529", "530", "531", "532", "533", "534",
"535", "536", "537", "538", "539", "540", "541", "542", "543",
"544", "545", "546", "547", "548", "549", "550"), class = "factor")), .Names = c("Vial_nr",
"Concentration", "Line", "Sex", "Fly", "Temp", "X0.5_sec", "X1_sec",
"X1.5_sec", "X2_sec", "X2.5_sec", "X3_sec", "Vial"), row.names = 4:7, class = "data.frame")
dat <- read.table("Complete RING.txt", header =TRUE)
str(dat)
dat$Vial <- as.factor(dat$Vial)
dat$Line <- as.factor(dat$Line)
dat$Fly <- as.factor(dat$Fly)
dat$Temp <- as.factor(dat$Temp)
str(dat)
Line20af <- subset(dat, Line=="20" & Concentration=="a" & Sex=="f")
Line20bf <- subset(dat, Line=="20" & Concentration=="b" & Sex=="f")
Line20cf <- subset(dat, Line=="20" & Concentration=="c" & Sex=="f")
Line20df <- subset(dat, Line=="20" & Concentration=="d" & Sex=="f")
Line20ef <- subset(dat, Line=="20" & Concentration=="e" & Sex=="f")
Line20xf <- subset(dat, Line=="20" & Concentration=="x" & Sex=="f")
Line20yf <- subset(dat, Line=="20" & Concentration=="y" & Sex=="f")
out20 <- matrix(ncol=6, nrow=7)
colnames(out20) <- colnames(Line20af)[7:12]
rownames(out20) <- paste0("Concentration", 1:7)
for(i in 1:6){
tmp <- data.frame(Line20af[,6+i],Line20af$Temp)
colnames(tmp)<-c("y","c")
fit1=lm(y~c,data=tmp)
fit2=lm(y~1,data=tmp)
out20[1,i]<-anova(fit1,fit2)$"Pr(>F)"[2]
tmp <- data.frame(Line20bf[,6+i],Line20bf$Temp)
colnames(tmp)<-c("y","c")
fit1=lm(y~c,data=tmp)
fit2=lm(y~1,data=tmp)
out20[2,i]<-anova(fit1,fit2)$"Pr(>F)"[2]
tmp <- data.frame(Line20cf[,6+i],Line20cf$Temp)
colnames(tmp)<-c("y","c")
fit1=lm(y~c,data=tmp)
fit2=lm(y~1,data=tmp)
out20[3,i]<-anova(fit1,fit2)$"Pr(>F)"[2]
tmp <- data.frame(Line20df[,6+i],Line20df$Temp)
colnames(tmp)<-c("y","c")
fit1=lm(y~c,data=tmp)
fit2=lm(y~1,data=tmp)
out20[4,i]<-anova(fit1,fit2)$"Pr(>F)"[2]
tmp <- data.frame(Line20ef[,6+i],Line20ef$Temp)
colnames(tmp)<-c("y","c")
fit1=lm(y~c,data=tmp)
fit2=lm(y~1,data=tmp)
out20[5,i]<-anova(fit1,fit2)$"Pr(>F)"[2]
tmp <- data.frame(Line20xf[,6+i],Line20xf$Temp)
colnames(tmp)<-c("y","c")
fit1=lm(y~c,data=tmp)
fit2=lm(y~1,data=tmp)
out20[6,i]<-anova(fit1,fit2)$"Pr(>F)"[2]
tmp <- data.frame(Line20yf[,6+i],Line20yf$Temp)
colnames(tmp)<-c("y","c")
fit1=lm(y~c,data=tmp)
fit2=lm(y~1,data=tmp)
out20[7,i]<-anova(fit1,fit2)$"Pr(>F)"[2]
}
xtable(out20)
但这样做很多次只是复制粘贴似乎真的很愚蠢,我试图制作临时数据字段所以我没有得到许多数据集,但是这并没有产生任何结果。我还尝试使用浓度上的unique()创建一个for循环来创建新的数据集但是R不会打印任何结果。有没有办法优化这段脚本来简化我的工作流程而不是溢出我的工作区?
编辑:添加数据剪辑