【发布时间】:2019-03-05 04:40:00
【问题描述】:
我有一个相对简单的问题,我无法应用我在互联网上找到的解决方案。假设我们有:
set.seed(20)
data <- data.frame(month = rep(month.name, 25),
a = rnorm(300, 0, 1), b = runif(300, 0, 7.2))
我想使用循环计算 a 和 b 列在 month 中每个月的差异的 f 检验。这是我通过使用完成的:
# create some empty vectors to fill in later
pval <- as.double()
ftest <- as.double()
month <- as.character()
# looping through the months
for (i in unique(data$month)){
print(i)
# sh.1 <- shapiro.test(data$a[data$month==i])
# sh.1[2] > 0.05 # apply log if it's smaller than 0.05
# sh.2 <- shapiro.test(data$b[data$month==i])
# sh.2[2] > 0.05 # apply log if it's smaller than 0.05
var.t <- var.test(data$a[data$month==i], data$b[data$month==i])
f <- round(var.t[[1]],2)
p <- round(var.t$p.value,2)
ftest <- append(ftest, f)
pval <- append(pval, p)
month <- append(month, i)
}
但是,据我所知,f-test 对正态分布非常敏感。因此,我计划在循环中使用条件,如果 shapiro 测试的 p 值小于 0.05,则需要对数据进行 log 转换;然后它将用于 f-test。
通常,我会在 ifelse 条件下执行此操作,但我不太确定如何在这里使用它。 请问这里有什么帮助吗?
【问题讨论】:
-
您确定数据是正数吗?只能针对正值计算日志,并且您的列
a是从法线中提取的。 -
是的,数据是肯定的。为了使示例更具代表性,我还创建了一个具有标准化值的列。
标签: r loops if-statement normal-distribution