r - Pairwise t test using plyr -
i use r package plyr run pairwise t test on large data frame, i'm not sure how it. learned how correlations using plyr, , how can specify groups want compare , plyr breaks down data you. example, have plyr calculate correlation between sepal length , sepal width each species of iris in iris dataset this:
correlations <- ddply(iris, "species", function(x) cor(x$sepal.length, x$sepal.width))
i could break data frame down myself specifying data setosa species of iris in rows 1:50 , on, plyr less me mess , accidentally rows 1:51, example.
so how do similar paired t test? how can specify observations pairs? here's example data similar i'm working with, , i'd pairs subject , i'd break data down pesticide:
exposure <- data.frame("subject" = rep(1:4, 6), "season" = rep(c(rep("summer", 4), rep("winter", 4)),3), "pesticide" = rep(c("atrazine", "metolachlor", "chlorpyrifos"), each=8), "exposure" = sample(1:100, size=24)) exposure$subject <- as.factor(exposure$subject)
in other words, question i'd evaluate whether there difference in pesticide exposure each person during winter versus during summer, , i'd answer question separately each of 3 pesticides.
much in advance!
an edit: clarify, how unpaired t test in plyr:
ttests <- dlply(exposure, "pesticide", function(x) t.test(x$exposure ~ x$season))
and if add "paired=t" in there, plyr will do paired t test, assumes have pairs in same order. while have them in same order in example data frame above, don't in real data because have missing data.
do want this?
library(data.table) # convert data.table in place setdt(exposure) # make sure data sorted correctly setkey(exposure, pesticide, season, subject) exposure[, list(res = list(t.test(exposure[season == "summer"], exposure[season == "winter"], paired = t))) , = pesticide]$res #[[1]] # # paired t-test # #data: exposure[season == "summer"] , exposure[season == "winter"] #t = -4.1295, df = 3, p-value = 0.02576 #alternative hypothesis: true difference in means not equal 0 #95 percent confidence interval: # -31.871962 -4.128038 #sample estimates: #mean of differences # -18 # # #[[2]] # # paired t-test # #data: exposure[season == "summer"] , exposure[season == "winter"] #t = -6.458, df = 3, p-value = 0.007532 #alternative hypothesis: true difference in means not equal 0 #95 percent confidence interval: # -73.89299 -25.10701 #sample estimates: #mean of differences # -49.5 # # #[[3]] # # paired t-test # #data: exposure[season == "summer"] , exposure[season == "winter"] #t = -2.5162, df = 3, p-value = 0.08646 #alternative hypothesis: true difference in means not equal 0 #95 percent confidence interval: # -30.008282 3.508282 #sample estimates: #mean of differences # -13.25
Comments
Post a Comment