加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在R的ezANOVA函数中动态定义依赖变量和独立变量

发布时间:2020-12-14 04:55:06 所属栏目:百科 来源:网络整理
导读:我想从循环中的多个因变量的ez包中运行ezANOVA,并将结果保存到多个变量中.每个因变量都在同一数据帧的单独列中. all.dependent.variables - c("dv1","dv2")for(dependent.variable in all.dependent.variables){ assign(paste(dependent.variable,".aov.resu
我想从循环中的多个因变量的ez包中运行ezANOVA,并将结果保存到多个变量中.每个因变量都在同一数据帧的单独列中.

all.dependent.variables <- c("dv1","dv2")

for(dependent.variable in all.dependent.variables){
  assign(paste(dependent.variable,".aov.results",sep = ""),ezANOVA(aov.data,dv = dependent.variable,wid = subject,within = .(factor1,factor2),return_aov = TRUE))
}

这是一个示例数据框:

aov.data <- structure(list(subject = structure(c(10L,11L,12L,1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,9L),.Label = c("1","2","3","4","5","6","7","8","9","10","11","12"),class = "factor"),dv1 = c(650.2,773.7,686.4,436.2,625.3,714.2,892.6,921.5,711.2,670.2,725.8,592.8,672.7,731.1,707.2,475.1,645.4,786.7,949.5,925.8,715.5,745.4,750.8,579.1,683.3,707.6,693.7,492.4,698.8,666.9,914.4,853.8,724.4,718.8,872.9,616.9,706.4,766.2,676.2,500,753.8,712.7,1012.2,947.8,695.3,735.6,843.7,596.1,738.3,705.2,718.2,534.1,805.3,814.1,969.4,1010.7,-999,714.4,815.4,835.4,830.7,776.7,543.7,757.2,841.5,1107.8,915.8,707.4,809.7,671.1,638.1,726.7,660.2,455.7,623.5,716.1,922.1,804.5,674.6,797.4,572,676.7,726.6,690.7,498.8,624.3,764.1,889.5,823.4,672.9,701.8,750.4,557.2,656.1,701,655.1,472.7,658.8,660.6,860.9,811.3,672.5,681.7,849.6,571.2,694,777.5,661.3,488,670.4,725.3,938.1,862.7,616.4,732.2,845.9,582.4,694.2,694.6,743.8,480.5,736.7,740.9,988.1,827.5,812.4,725.5,844.2,628,779.3,770.3,686.9,494.3,681.5,850.5,990.7,810.1,692.3,779.7,779.8,590.4,618.6,713.9,609.6,468.6,554.3,580.8,864.1,843.3,662.8,645.5,714.6,555.5,670.7,759.3,652.2,468.1,613.5,712.3,910.7,782.4,723.3,742.8,775.5,553.2,726.2,591.2,479.2,626.1,643.3,821.5,753.9,818.2,655.8,754.4,592.9,703.5,792.5,635,485.3,644.1,667.9,891.3,780.9,699.1,725.1,716,587.2,706.5,754.6,694.3,485.5,745.5,649.3,808.4,780.5,773.8,676.3,687.5,685.3,910.4,738.5,525,689.6,758.4,1021.5,792,789.3,740.5,722.8,717.1,653.3,743.6,620.3,460.1,575.3,647.1,849.3,647,691.2,596.4,531.6,678.7,754.7,600.4,463.8,560.8,636.6,844.3,766.6,628.7,784.4,547.9,630.3,656.7,705.3,443.3,607,630.7,861.5,770.8,664.5,728.3,546.4,741.5,620.4,459.3,587.9,626.2,893.8,756.1,731.8,680.2,836.4,566.7,619.4,704,445.3,652.7,735.3,839.4,833.4,763.7,614.5,794.4,562.5,713.2,735.4,655.4,501.1,635.6,661.2,880.6,747.8,807.8,757.7,772.4,560.1,662.7,682.5,590.1,443.6,623.2,656.6,852.6,676.8,646.6,646,677.6,518,664,665.4,609.8,464.2,696.5,661,894.7,661.1,659.6,657.8,713.4,531.5,739.5,695.1,656.5,498.4,648.1,710,897.8,685,671.3,657,767.5,545.3,808.6,697.5,667.2,463.4,652.4,857.2,690.3,766.1,696.1,690.5,558.8,746,708.4,690,515.5,788.8,929.6,802.1,619.5,510.8,654.1,811.8,977.8,697.9,700.9,497.9,811.5,969.3,723,886,815.7,757.5,639.5,688.4,617.8,435.2,628.8,603.3,865.3,661.6,645.9,598.1,646.8,477.2,760.8,634.3,452.2,600.1,648.2,923.8,625.5,676.9,647.3,688.6,513.2,591.9,641.6,632.6,469.6,606.4,610.9,835.1,667.8,599.7,581.2,704.4,502.9,746.9,684.1,689.3,475.9,692,689,824.9,625.9,696.4,706.3,715.3,510.9,650.9,640.1,663.5,471.6,682.3,683.2,831.9,702,624.6,698.2,521.6,759.8,730.6,473.2,644.4,738.7,932.5,685.1,816,722.1,783.3,526.2,0),dv2 = c(2.941,2.941,5.882,8.824,23.529,35.294,17.647,44.118,29.412,20.588,11.765,26.471,14.706,47.059,38.235,52.941,61.765,55.882,94.118,41.176,32.353,85.294,76.471,50,67.647,73.529,82.353,factor1 = structure(c(1L,3L),.Label = c("level1","level2","level3"),factor2 = structure(c(1L,2L),"level3"
),class = "factor")),.Names = c("subject","dv1","dv2","factor1","factor2"),row.names = c(NA,540L),class = "data.frame")

这个问题是,R将索引dependent.variable解释为数据帧aov.data中列的说明符,因此返回以下错误:

“dependent.variable” is not a variable in the data frame provided.

我尝试用eval()或print()包装索引,但无济于事.

解决方法

好吧,这绝对不是你的错…… ezANOVA在内部做了一些聪明的评估,搞砸了你.这是一个似乎有用的可怕咒语,但最好是(1)联系包维护者,看看他们是否能找到更优雅地修复内部的方法; (2)考虑是否可以通过一个更标准的anova评估器来解决这个问题.

fitlist <- lapply(all.dependent.variables,function(x) {
           e1 <- expression(ezANOVA(aov.data,dv = x,return_aov = TRUE))
           ## now force evaluation of the "dv" component of the call
           e1[[1]][["dv"]] <- eval(e1[[1]][["dv"]])
           eval(e1)
       })

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读