I have pollution index (PI) data collected from 5 sites (BS, PL, GB, EM and CB). Now I want to run an ANOVA test in order to compare sites by using EM as the reference site.
我有从5个站点(BS,PL,GB,EM和CB)收集的污染指数(PI)数据。现在我想运行ANOVA测试,以便使用EM作为参考站点来比较站点。
How can I do this using r?
我怎么能用r做这个?
Here's some of my data:
这是我的一些数据:
Site PI
BS 0.229056825
BS 0.217159476
BS 0.229508197
BS 0.127530364
BS 0.112942122
PL 0.198957428
PL 0.196986402
PL 0.126753976
PL 0.092816275
PL 0.143828265
GB 0.26883746
GB 0.156452227
GB 0.206349206
GB 0.214451538
GB 0.216014235
EM 0.192636423
EM 0.164269912
EM 0.181659896
EM 0.161620295
EM 0.145764576
CB 0.38490566
CB 0.111111111
CB 0.092592593
CB 0.40625
CB 0.68852459
1 个解决方案
#1
ANOVA by linear regression is an easy way and anova()
in the stats package accepts a lm object.
线性回归的ANOVA是一种简单的方法,stats包中的anova()接受lm对象。
Assuming that your data is in a data frame called as df, the following shows a way.
假设您的数据位于名为df的数据框中,以下显示了一种方法。
It loops over sites except for the reference site (EM) and do the ANOVA test, followed by fitting linear regression. A list of sites and anova results is returned by the loop (comparison).
它在除参考位点(EM)之外的位点上循环并进行ANOVA测试,然后拟合线性回归。循环(比较)返回站点和anova结果的列表。
In the console output, the first element is the site that's compared (BS) and the second element is the ANOVA result.
在控制台输出中,第一个元素是比较(BS)的站点,第二个元素是ANOVA结果。
# reference PI
ref <- df[df$Site=="EM", 2]
# site names except for EM, used to filter df
sites <- unique(df$Site)[unique(df$Site) != "EM"]
comparison <- lapply(sites, function(x) {
# ANOVA for comparion
fit <- lm(ref ~ df[df$Site==x, 2])
list(x, anova(fit))
})
comparison[[1]]
#[[1]]
#[1] BS
#Levels: BS CB EM GB PL
#[[2]]
#Analysis of Variance Table
#Response: ref
#Df Sum Sq Mean Sq F value Pr(>F)
#df[df$Site == x, 2] 1 0.00093945 0.00093945 7.1162 0.07584 .
#Residuals 3 0.00039605 0.00013202
#---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#1
ANOVA by linear regression is an easy way and anova()
in the stats package accepts a lm object.
线性回归的ANOVA是一种简单的方法,stats包中的anova()接受lm对象。
Assuming that your data is in a data frame called as df, the following shows a way.
假设您的数据位于名为df的数据框中,以下显示了一种方法。
It loops over sites except for the reference site (EM) and do the ANOVA test, followed by fitting linear regression. A list of sites and anova results is returned by the loop (comparison).
它在除参考位点(EM)之外的位点上循环并进行ANOVA测试,然后拟合线性回归。循环(比较)返回站点和anova结果的列表。
In the console output, the first element is the site that's compared (BS) and the second element is the ANOVA result.
在控制台输出中,第一个元素是比较(BS)的站点,第二个元素是ANOVA结果。
# reference PI
ref <- df[df$Site=="EM", 2]
# site names except for EM, used to filter df
sites <- unique(df$Site)[unique(df$Site) != "EM"]
comparison <- lapply(sites, function(x) {
# ANOVA for comparion
fit <- lm(ref ~ df[df$Site==x, 2])
list(x, anova(fit))
})
comparison[[1]]
#[[1]]
#[1] BS
#Levels: BS CB EM GB PL
#[[2]]
#Analysis of Variance Table
#Response: ref
#Df Sum Sq Mean Sq F value Pr(>F)
#df[df$Site == x, 2] 1 0.00093945 0.00093945 7.1162 0.07584 .
#Residuals 3 0.00039605 0.00013202
#---
# Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1