- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用组标识符 g
在 (y1,...,yN) 上对 ANOVA 进行排列检验.我应该使用 (1)/(g-1) (muhatj - muhat)^2 的总和作为测试统计量,而 muhatj 是第 j 组样本均值,和 muhat=(1/g)summation muhatj.
## data
y <- c(6.59491, 6.564573, 6.696147, 6.321552, 6.588449, 6.853832,
6.370895, 6.441823, 6.227591, 6.675492, 6.255462, 6.919716, 6.837458,
6.41374, 6.543782, 6.562947, 6.570343, 6.993634, 6.666261, 7.082319,
7.210933, 6.547977, 6.330553, 6.309289, 6.913492, 6.597188, 6.247285,
6.644366, 6.534671, 6.885325, 6.577568, 6.499041, 6.827574, 6.198853,
6.965038, 6.58837, 6.498529, 6.449476, 6.544842, 6.496817, 6.499526,
6.709674, 6.946934, 6.23884, 6.517018, 6.206692, 6.491935, 6.039925,
6.166948, 6.160605, 6.428338, 6.564948, 6.446658, 6.566979, 7.17546,
6.45031, 6.612242, 6.559798, 6.568082, 6.44193, 6.295211, 6.446384,
6.658321, 6.369639, 6.066747, 6.345537, 6.727513, 6.677873, 6.889841,
6.724438, 6.379956, 6.380779, 6.50096, 6.676555, 6.463236, 6.239091,
6.797642, 6.608025)
## group
g <- structure(c(2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 2L,
3L, 2L, 3L, 2L, 3L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 1L, 3L, 1L, 2L, 2L, 1L, 3L, 2L, 2L, 3L, 1L, 2L, 2L, 2L, 1L,
2L), .Label = c("B1", "B2", "B3"), class = "factor")
这是我现在拥有的,但是当我更改它以测试样本均值而不是 F 统计量时,它不起作用。我很确定我需要将 T.obs
和 T.perm
更改为类似于 by(y, g, mean)
的内容但我认为我还缺少更多。
n <- length(y) #sample size n
T.obs<- anova(lm(y ~ g))$F[1] #Observed statistic
n.perm <- 2000 # we will do 2000 permutations
T.perm <- rep(NA, n.perm) #A vector to save permutated statistic
for(i in 1:n.perm) {
y.perm <- sample(y, n, replace=F) #permute data
T.perm[i] <- anova(lm(y.perm ~ g))$F[1] #Permuted statistic
}
mean(T.perm >= T.obs) #p-value
最佳答案
我真的不知道“它不工作”是什么意思。据我所知,它工作正常,只是有点慢。
set.seed(0)
n <- length(y) #sample size n
T.obs <- anova(lm(y ~ g))$F[1] #Observed statistic
n.perm <- 2000 # we will do 2000 permutations
T.perm <- rep(NA, n.perm) #A vector to save permutated statistic
for(i in 1:n.perm) {
y.perm <- sample(y, n, replace=F) #permute data
T.perm[i] <- anova(lm(y.perm ~ g))$F[1] #Permuted statistic
}
mean(T.perm >= T.obs)
# [1] 0.4915
这与理论值相当接近
anova(lm(y ~ g))$Pr[1]
# [1] 0.4823429
所以,是的,你做的都是正确的!
从您问题的第一段来看,我们似乎想自己计算 F 统计量,所以下面的函数就是这样做的。有一个开关 "use_lm"
。如果设置 TRUE
,它使用 anova(lm(y ~ g))
作为您在原始代码中所做的。 此函数旨在使 F 统计量和 p 值的计算变得透明。此外,手动计算比调用 lm
和 anova
快 15 倍(这是显而易见的...)。
fstat <- function (y, g, use_lm = FALSE) {
if (!use_lm) {
## group mean (like we are fitting a linear model A: `y ~ g`)
mu_g <- ave(y, g, FUN = mean)
## overall mean (like we are fitting a linear model B: `y ~ 1`)
mu <- mean(y)
## RSS (residual sum of squares) for model A
RSS_A <- drop(crossprod(y - mu_g))
## RSS (residual sum of squares) for model B
RSS_B <- drop(crossprod(y - mu))
## increase of RSS from model A to model B
RSS_inc <- RSS_B - RSS_A
## note, according to "partition of squares", we can also compute `RSS_inc` as
## RSS_inc <- drop(crossprod(mu_g - mu))
## `sigma2` (estimated residual variance) of model A
sigma2 <- RSS_A / (length(y) - nlevels(g))
## F-statistic
fstatistic <- ( RSS_inc / (nlevels(g) - 1) ) / sigma2
## p-value
pval <- pf(fstatistic, nlevels(g) - 1, length(y) - nlevels(g), lower.tail = FALSE)
## retern
return(c(F = fstatistic, pval = pval))
}
else {
anovalm <- anova(lm(y ~ g))
return(c(F = anovalm$F[1L], pval = anovalm$Pr[1L]))
}
}
让我们先检查一下这个函数的有效性:
F_obs <- fstat(y, g)
# F pval
#0.7362340 0.4823429
F_obs <- fstat(y, g, TRUE)
# F pval
#0.7362340 0.4823429
不要因为它微不足道而感到惊讶。您的数据并没有真正表明存在显着的群体差异。看看箱线图:
boxplot(y ~ g) ## or use "factor" method of `plot` function: `plot(g, y)`
现在我们继续排列。我们为此编写了另一个函数 perm
。这实际上很容易,因为我们有一个很好定义的 fstat
。我们需要做的就是使用replicate
来包装sample
+ fstat
。
lm
实际上很慢:
library(microbenchmark)
microbenchmark(fstat(y, g), fstat(y, g, TRUE), times = 200)
#Unit: microseconds
# expr min lq mean median uq max neval cld
# fstat(y, g) 228.44 235.32 272.1204 275.34 290.20 388.84 200 a
# fstat(y, g, TRUE) 4090.00 4136.72 4424.0470 4181.02 4450.12 16460.72 200 b
所以我们使用 f(..., use_lm = FALSE)
编写此函数:
perm <- function (y, g, n) replicate(n, fstat(sample(y), g)[[1L]])
现在让我们用 n = 2000
来运行它(为再现性设置随机种子):
set.seed(0)
F_perm <- perm(y, g, 2000)
## estimated p-value based on permutation
mean(F_perm > F_obs[[1L]])
# [1] 0.4915
请注意它与理论 p 值的接近程度:
F_obs[[2L]]
# [1] 0.4823429
如您所见,结果与您的原始代码一致。
关于r - 基于置换法的方差分析中 F 统计量的 Monte Carlo 估计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40336661/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我有实体: @Entity @Table(name = "CARDS") public class Card { @ManyToOne @JoinColumn(name = "PERSON_I
我正在尝试计算二维多边形的表面法线。我正在使用 OpenGL wiki 中的 Newell 方法来计算表面法线。 https://www.opengl.org/wiki/Calculating_a_S
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 7 年前。 Improve
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我这里有以下 XML: Visa, Mastercard, , , , 0, Discover, American Express siteonly, Buyer Pay
即将发生的 Google 政策变更迫使我们实现一个对话框,以通知欧盟用户有关 Cookie/设备标识符用于广告和分析的情况。我只想向欧盟用户显示此对话框。我不想使用额外的权限(例如 android.p
本文分享自华为云社区《华为大咖说 | 企业应用AI大模型的“道、法、术” ——道:认知篇》,作者:华为云PaaS服务小智。 本期核心观点 上车:AGI是未来5~10年内,每个人都无法回避的技
我有一个与酒精相关的网站,需要先验证年龄,然后才能让他们进入该网站。我使用 HttpModule 来执行此操作,该模块检查 cookie,如果未设置,我会将它们重定向到验证页面。我验证他们的年龄并存储
在欧盟,我们有一项法律,要求网页请求存储 cookie 的许可。我们大多数人都了解 cookie 并同意它们,但仍然被迫在任何地方明确接受它们。所以我计划编写这个附加组件(ff & chrome),它
以下在 C 和/或 C++ 中是否合法? void fn(); inline void fn() { /*Do something here*/ } 让我担心的是,第一个声明看起来暗示函数将被定义
我是一名优秀的程序员,十分优秀!