【问题标题】:'names' attribute must be the same length as vector'names' 属性必须与向量的长度相同
【发布时间】:2019-12-26 21:41:30
【问题描述】:

我正在尝试绘制我的回归分析

我创建了一个 glm 模型来确定农业对鸟类物种丰富度的影响。

这是我的 glm 代码:​​

spec_rich_final <- glm(species_richness ~ (trans_forest * fert_total) + 
agri + I(trans_agri^2) + diversity + (I(diversity^2)) + (I(trans_forest^2) 
* fert_total) + trans_semi_natural + bio1, poisson(link = "log"), 
data=data_transformed)

那么这是我的绘图代码:

effect_plot(spec_rich_final, pred = agri, interval = TRUE, 
partial.residuals = TRUE)

然后我得到这个错误:

Error in names(o) <- rownames(attr(terms(formula), "factors")) : 
  'names' attribute [10] must be the same length as the vector [8]

有人可以帮忙吗?不知道属性 10 或向量 8 是什么意思??

【问题讨论】:

  • 错误是关于长度不匹配。 names 属性的长度为 10,但您尝试分配给它的向量的长度为 8。除此之外,如果没有复制此数据的样本,我们将无法运行您的代码
  • @camille 什么是长度?有没有办法可以附加我的代码?另外 - 当我运行绘图以绘制原始数据时,它工作正常,但不适用于残差。在论文中显示原始数据或残差图会更好吗?
  • 向量的长度。就像那个向量中的东西的数量一样。至于你在一篇论文中需要什么:这完全取决于上下文、主题、发表地点、分析要求、分析类型……所以,没有线索。 See here 写一篇好的 R 帖子。您可以(并且应该)发布帮助调试所需的代码
  • effect_plot 来自什么包?也许str(spec_rich_final)str(agri) 就足够了。但正如夏米尔所说,它们的长度肯定不同,问题可能出在其他地方。一步一步的橡皮鸭调试是为了。

标签: r plot attributes glm lm


【解决方案1】:

发生此错误是因为您正在使用:

glm(species_richness ~ ... + agri + I(trans_agri^2) + diversity + (I(diversity^2)))

改为使用

glm(species_richness ~ ... + poly(agri, 2) + poly(diversity, 2))

在 effect_plot 函数中添加:

data=data_transformed

【讨论】:

    猜你喜欢
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-18
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多