0

I am trying to plot my regression analyses

I have created a glm model to determine the effect of agriculture on species richness of birds.

this is my code for my 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)

then this is my code for plotting:

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

I then get this error:

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

Can anyone help? no idea what it means by attribute 10 or vector 8??

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    The error is about lengths not matching. The names attribute has a length of 10, but the vector you've tried to assign to it has a length 8. Beyond that, we can't run your code without a sample of data that replicates this – camille Aug 21 '19 at 15:13
  • @camille what is a length though? is there a way I can attach my code? Also - when I run the plot for plotting raw data it works fine but not for residuals. In a paper would it be better to show a plot of raw data or residuals do you know? – Bex Jenkins Aug 21 '19 at 16:43
  • The length of a vector. Like the number of things in that vector. As for what you need in a paper: that totally depends on context, subject, where it's being published, requirements of analysis, type of analysis...so, no clue. [See here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making a good R post. You can (and should) post code needed to help debug – camille Aug 21 '19 at 17:01
  • What package does `effect_plot` come from? Perhaps `str(spec_rich_final)` and `str(agri)` would be sufficient. But as chamille said, their lengths differ for sure and the problems is probably somewhere else. A step-by-step rubber duck debugging is in order. – Roman Luštrik Aug 22 '19 at 09:12

1 Answers1

2

This error happens because you are using:

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

Instead use

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

In the effect_plot function add:

data=data_transformed
yuki
  • 745
  • 4
  • 15
Regan
  • 21
  • 2