首页 > 解决方案 > 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?

标签: rphylogeny

解决方案


您的解决方案是使用caperobject$data[,2] ~ caperobject$data[,5],因为comparative.dataclass 是一个列表,其中的特征值位于列表中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)


推荐阅读