fligner.test 在 R 上用于多列
fligner.test on R for multiple columns
我今天大部分时间都在研究这个问题,但在网上找不到任何地方的答案。
我有一组数据,其中包含两个因子变量和 30 个因变量。我想根据我的两个因子变量之间的相互作用,使用 fligner 检验来确定每个因变量是否满足等方差假设。
我可以一次对一个变量执行此操作并获得 p 值:
fligner=fligner.test(variable~interaction(factor1,factor2),data=mydata)
fligner$p.value`
但我无法同时对所有变量执行此操作。我试过 lapply(这是我用来获取所有 shapiro.test 数据的方法)。这是我的代码:
#Do the regressions and get residuals for all variables
variables <- as.matrix( mydata[,x:y] )
allfits<-lm(variables~Drug*Pollutant,data=mydata)
allresiduals<-residuals(allfits)
#Shapiro test on all of it
residuals<-as.data.frame(allresiduals)
lshap <- lapply(residuals, shapiro.test)
lres <- sapply(lshap, `[`, c("p.value"))
lres
请帮忙!这让我发疯。
我试过了,但没用:
fligners<-fligner.test(variables~interaction(Pollutant,Drug),data=mydata)
我收到此错误: fligner.test.default(c(1.06, 0.98, 0.94, 0.95, 1.08, 0.95, 0.76, :
'x' 和 'g' 必须具有相同的长度
这是一个例子
data(CO2)
mydata <- CO2
# columns to apply fligner test to
cols <- 4:5
# interaction columns
factor_cols <- 2:3
# apply test
fligner <- sapply(cols, function(x) {
fligner.test(mydata[,x] ~
interaction(mydata[,factor_cols[1]], mydata[,factor_cols[2]]))
})
# get p-values
p.values <- apply(fligner, 2, function(x) {
x$p.value
})
# p.values = 1.0000000 0.2983002
当然我的数据集不适合这个假设检验。但是,第一次测试的 p 值为 1,第二次测试的 p 值为 0.2983。
有点老派 :) 但这行得通:
# Generate some data
testData <- data.frame(factor1 = sample(LETTERS[1:6], 100, T),
factor2 = sample(LETTERS[7:10], 100, T),
var1 = rnorm(100), var2 = rnorm(100), var3 = rnorm(100))
# Create formulae - I've used all columns starting var* as the Dependant Variables
formulae <- paste( grep("^var", names(testData), value = TRUE), " ~ interaction(factor1, factor2)")
# Apply the function
pValues <- sapply(formulae, function(F, DAT) fligner.test(as.formula(F), data = DAT)$p.value, DAT = testData)
pValues
按照 "be kinder to future you" 的哲学...
像这样拆分可能更清晰
# Funtion that performs a fligner test and returns a P value
flignerP <- function(V, data) {
theForm <- as.formula(paste(V, "~ interaction(factor1, factor2)"))
fligner.test(theForm, data = data)$p.value
}
# Perform the test on the var* variables
theVars <- grep("^var", names(testData), value = TRUE)
sapply(theVars, flignerP, data = testData)
我今天大部分时间都在研究这个问题,但在网上找不到任何地方的答案。
我有一组数据,其中包含两个因子变量和 30 个因变量。我想根据我的两个因子变量之间的相互作用,使用 fligner 检验来确定每个因变量是否满足等方差假设。
我可以一次对一个变量执行此操作并获得 p 值:
fligner=fligner.test(variable~interaction(factor1,factor2),data=mydata)
fligner$p.value`
但我无法同时对所有变量执行此操作。我试过 lapply(这是我用来获取所有 shapiro.test 数据的方法)。这是我的代码:
#Do the regressions and get residuals for all variables
variables <- as.matrix( mydata[,x:y] )
allfits<-lm(variables~Drug*Pollutant,data=mydata)
allresiduals<-residuals(allfits)
#Shapiro test on all of it
residuals<-as.data.frame(allresiduals)
lshap <- lapply(residuals, shapiro.test)
lres <- sapply(lshap, `[`, c("p.value"))
lres
请帮忙!这让我发疯。
我试过了,但没用:
fligners<-fligner.test(variables~interaction(Pollutant,Drug),data=mydata)
我收到此错误: fligner.test.default(c(1.06, 0.98, 0.94, 0.95, 1.08, 0.95, 0.76, : 'x' 和 'g' 必须具有相同的长度
这是一个例子
data(CO2)
mydata <- CO2
# columns to apply fligner test to
cols <- 4:5
# interaction columns
factor_cols <- 2:3
# apply test
fligner <- sapply(cols, function(x) {
fligner.test(mydata[,x] ~
interaction(mydata[,factor_cols[1]], mydata[,factor_cols[2]]))
})
# get p-values
p.values <- apply(fligner, 2, function(x) {
x$p.value
})
# p.values = 1.0000000 0.2983002
当然我的数据集不适合这个假设检验。但是,第一次测试的 p 值为 1,第二次测试的 p 值为 0.2983。
有点老派 :) 但这行得通:
# Generate some data
testData <- data.frame(factor1 = sample(LETTERS[1:6], 100, T),
factor2 = sample(LETTERS[7:10], 100, T),
var1 = rnorm(100), var2 = rnorm(100), var3 = rnorm(100))
# Create formulae - I've used all columns starting var* as the Dependant Variables
formulae <- paste( grep("^var", names(testData), value = TRUE), " ~ interaction(factor1, factor2)")
# Apply the function
pValues <- sapply(formulae, function(F, DAT) fligner.test(as.formula(F), data = DAT)$p.value, DAT = testData)
pValues
按照 "be kinder to future you" 的哲学...
像这样拆分可能更清晰# Funtion that performs a fligner test and returns a P value
flignerP <- function(V, data) {
theForm <- as.formula(paste(V, "~ interaction(factor1, factor2)"))
fligner.test(theForm, data = data)$p.value
}
# Perform the test on the var* variables
theVars <- grep("^var", names(testData), value = TRUE)
sapply(theVars, flignerP, data = testData)