r - PGLS returns an error when referring to variables by their column position in a caper object
问题描述
I am carrying out PGLS
between a trait and 21 environmental variables for a clade of plant species. I am using a loop to do this 21 times, once for each of the environmental variables, and extract the p-values and some other values into a results matrix.
When normally carrying each PGLS
individually I will refer to the variables by their column names, for example:
pgls(**trait1**~**meanrainfall**, data=caperobject)
But in order to loop this process for multiple environmental variables, I am referring to the variables by their column position in the data frame (which is in the form of the caper object for PGLS) instead of their column name:
pgls(**caperobject[,2]**~**caperobject[,5]**, data=caperobject)
This returns the error:
Error in model.frame.default(formula, data$data, na.action = na.pass) :
invalid type (list) for variable 'caperobject[, 2]'
This is not a problem when running a linear regression using the original data frame -- referring to the variables by their column name only produces this error when using the caper object as the data using PGLS
. Does this way of referring to the column names not work for caper objects? Is there another way I could refer to the column names so I can incorporate them into a PGLS loop
?
解决方案
您的解决方案是使用caperobject$data[,2] ~ caperobject$data[,5]
,因为comparative.data
class 是一个列表,其中的特征值位于列表中data
。这是一个例子:
library(ape)
library(caper)
# generate random data
seed <- 245937
tr <- rtree(10)
dat <- data.frame(taxa = tr$tip.label,
trait1 = rTraitCont(tr, root.value = 3),
meanrainfall = rnorm(10, 50, 10))
# prepare a comparative.data structure
caperobject <- comparative.data(tr, dat, taxa, vcv = TRUE, vcv.dim = 3)
# run PGLS
pgls(trait1 ~ meanrainfall, data = caperobject)
pgls(caperobject$data[, 1] ~ caperobject$data[, 2], data = caperobject)
对于截距 = 3.13 和斜率 = -0.003,两个选项返回相同的值。
解决数据格式问题的一个好习惯是检查数据是如何存储的str(caperobject)
。
推荐阅读
- python - 在Python中按日期过滤excel中的数据
- python - 如何在时间点计算 Pandas 中的累积 groupby 计数?
- ansible - 如何通过jinja2模板更新ansible角色的变量
- android - 单击按钮时想要转到下一个图像但应用程序不断崩溃
- python - 在 python 中复制 Postman POST-Request(元数据和文件上传)
- git - 在 cPanel Git™ 版本控制模块中看不到 Git 存储库
- apache-spark - BigQuery 将 NUMERIC 数据类型导出为 AVRO 中的二进制数据类型
- powershell - Powershell system.web.HttpUtility/system.UriBuilder 设置编码
- ios - 如何使用 .dSYM 文件部署 Xamarin iOS 应用程序
- reactjs - 使用 gatsby 和 netlify 预加载字体和图片的问题