如何使用effect()函数提取多个变量的边际均值? [英] How to extract marginal means of multiple variables with effect() function?

查看:249
本文介绍了如何使用effect()函数提取多个变量的边际均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的初学者,我想在包含200列以上结果变量的数据集中执行ANCOVA. 对我来说,最重要的是提取这些变量的p值和边际均值.我借助lapply()函数成功提取了p值,但是当我提取边际均值时出现了这样的错误Error in eval(predvars, data, env) : object 'x' not found.

I'm a beginner of R, and I would like to perform ANCOVA in a dataset with over 200 columns of outcome variables. The most important thing for me is to extract both p values and marginal means of these variables. I successfully extracted p values with the help of lapply() function, but when I extracted marginal means I got such error Error in eval(predvars, data, env) : object 'x' not found.

在这里,我使用内置数据集"iris"以显示我的问题为例.

Here I use the built-in dataset "iris" as an example to display my problem.

data("iris")

#load我要使用的软件包

#load packages that I would use

library(car); library(compute.es); library(effects); library(ggplot2);
library(multcomp); library(pastecs); library(WRS)

#set针对以下ANCOVA测试的对比:

#set contrasts for the following ANCOVA tests:

contrasts(iris$Species) <- contr.poly(3)

#同时对多个结果变量执行ANCOVA(这里,我比较了不同Specie级别的多个结果变量,以Petal.Width作为协变量)

#perform ANCOVA for multiple outcome variables at the same time (Here I compare multiple outcome variables at different Specie levels, with Petal.Width as the covariate)

list1 <- lapply(iris[, 1:3], function(x) Anova(aov(x ~ Petal.Width  + Species, data = iris), type="III"))
str(list1)

#extract主要测试的p值

#extract p values of the main tests

pvalues <- stack(lapply(iris[, 1:3], function(x) Anova(aov(x ~ Petal.Width  + Species, data = iris), type="III")[3, 4]))[2:1]

上面的代码很好用,但是当我使用effect()函数提取边际手段时,我得到了错误: #extract边际均值

The above code works well, but when I use effect() function to extract marginal means I got error: #extract marginal means

list2 <- lapply(iris[, 1:3], function(x) summary(effect("Species", aov(x ~ Petal.Width + Species, data = iris)), se=TRUE))

eval(predvars,data,env)中的错误:找不到对象'x'

Error in eval(predvars, data, env) : object 'x' not found

marginal.means <- stack(lapply(iris[, 1:3], function(x) summary(effect("Species", aov(x ~ Petal.Width + Species, data = iris)), se=TRUE)[[5]][[1]][1]))[2:1]

eval(predvars,data,env)中的错误:找不到对象'x'

Error in eval(predvars, data, env) : object 'x' not found

#当我提取某个变量(例如Sepal.Length)的边际均值时,不使用< lapply()时,它会起作用:

marginal.mean1 <- summary(effect("Species", aov(Sepal.Length ~ Petal.Width + Species, data = iris)), se=TRUE)
marginal.mean1

输出:

 Species
    setosa versicolor  virginica 
  5.880113   5.819859   5.830028 

 Lower 95 Percent Confidence Limits
Species
    setosa versicolor  virginica 
  5.490905   5.676927   5.485953 

 Upper 95 Percent Confidence Limits
Species
    setosa versicolor  virginica 
  6.269322   5.962791   6.174102

由于有200列以上的结果变量,我想一次提取边际均值,而不是一一提取它们.

Due to the over 200 columns of outcome variables, I would like to extract marginal means once rather than extracting them one by one.

非常感谢您的帮助,

Ella

推荐答案

您会收到此错误,因为函数effect()调用update()并尝试重新拟合模型,此时,它无法访问您的x了. (好的,也许我没有解释得太好).您可以阅读本书一章了解功能如何工作.

You get that error because the function effect() calls update() and tries to re-fit the model, and at that point, it cannot access your x anymore. (Ok maybe I did not explain that too well) You can read this book chapter to know how functions work.

尝试将所有内容保留在data.frame中,而是提供适合不同变量的公式:

Try to keep everything within the data.frame and instead provide the formula to fit a different variable:

list2 <- lapply(colnames(iris)[1:3], function(x){
anova_fit = aov(reformulate(c("Petal.Width","Species"),x), data = iris)
summary(effect("Species",anova_fit, se=TRUE))
})

如您所见,这也可以应用于您的其他功能.

As you can see, this can be applied to your other functions as well.

这篇关于如何使用effect()函数提取多个变量的边际均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆