Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

envfit() Error in object$weights #713

Closed
AlexaBennett opened this issue Nov 7, 2024 · 8 comments
Closed

envfit() Error in object$weights #713

AlexaBennett opened this issue Nov 7, 2024 · 8 comments

Comments

@AlexaBennett
Copy link

AlexaBennett commented Nov 7, 2024

When running envfit() with vegan_2.6-8, I received the following error message.

> Error in object$weights : $ operator is invalid for atomic vectors

The code performs as expected with the following session information:

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggforce_0.4.2       featuretable_0.0.11 lubridate_1.9.2    
 [4] forcats_1.0.0       stringr_1.5.1       dplyr_1.1.0        
 [7] purrr_1.0.2         readr_2.1.4         tidyr_1.3.1        
[10] tibble_3.2.1        ggplot2_3.4.1       tidyverse_2.0.0    
[13] vegan_2.6-4         lattice_0.22-6      permute_0.9-7     

The error is produced with:

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] vegan_2.6-4         lattice_0.22-6      permute_0.9-7       ggforce_0.4.2      
 [5] cowplot_1.1.3       viridis_0.6.5       viridisLite_0.4.2   featuretable_0.0.11
 [9] lubridate_1.9.3     forcats_1.0.0       stringr_1.5.1       dplyr_1.1.4        
[13] purrr_1.0.2         readr_2.1.5         tidyr_1.3.1         tibble_3.2.1       
[17] ggplot2_3.5.1       tidyverse_2.0.0     biplotr_0.0.13      magrittr_2.0.3     

and

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] vegan_2.6-8         lattice_0.22-6      permute_0.9-7       ggforce_0.4.2       cowplot_1.1.3      
 [6] viridis_0.6.5       viridisLite_0.4.2   featuretable_0.0.11 lubridate_1.9.3     forcats_1.0.0      
[11] stringr_1.5.1       dplyr_1.1.4         purrr_1.0.2         readr_2.1.5         tidyr_1.3.1        
[16] tibble_3.2.1        ggplot2_3.5.1       tidyverse_2.0.0     biplotr_0.0.13      magrittr_2.0.3 

Therefore, I believe a dependency update is causing the issue. Please advise.

@jarioksa
Copy link
Contributor

jarioksa commented Nov 7, 2024

Really hard to give an advice since I cannot reproduce this and there is no reproducible example. I do not even know if this error comes from vegan or from some other package. Element object$weights is only referenced to in fitspecaccum and stressplot.wcmdscale and neither of these is connected to envfit.

It would be best to have a reproducible example. In lack of that, the minimum is to have traceback() which returns the call stack. With that we could see what is the function that gives the error and how we got there.

@gavinsimpson
Copy link
Contributor

Only you (@AlexaBennett) can debug this; start from a clean session (R --vanilla) and run your vegan code. Add 1 package at a time and rerun your vegan code. Rinse and repeat until you find loading which package leads to the error.

@gavinsimpson gavinsimpson closed this as not planned Won't fix, can't repro, duplicate, stale Nov 7, 2024
@jarioksa jarioksa reopened this Nov 8, 2024
@gavinsimpson
Copy link
Contributor

@jarioksa not much point keeping this open until the OP posts something we can work with (they can still comment and edit the issue even when closed)

@gavinsimpson gavinsimpson closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2024
@jarioksa
Copy link
Contributor

jarioksa commented Nov 8, 2024

There is a point of keeping this open because I'm working on this. This looks like a permanent existing problem in stats:::weights.default which we have circumvented in many other functions in 3ddac18. We should handle those cases smoothly. After I get my trial fix done, I'll ask OP to test the new code.

@jarioksa jarioksa reopened this Nov 8, 2024
@gavinsimpson
Copy link
Contributor

Oops, sorry - I didn't realise you were actually working on this. Nobbling weights.default sounds... icky.

@jarioksa
Copy link
Contributor

jarioksa commented Nov 8, 2024

Here is a way to get the same error message (but I do not know if the OP has the same case):

library(vegan)
data(dune, dune.env)
mod <- cca(dune)
envfit(mod ~ ., dune.env) # this works
envfit(scores(mod, display="sites") ~ ., dune.env) # this fails, but should work
## Error in object$weights : $ operator is invalid for atomic vectors

In old code I solved this by replacing stats:::weights.default within function to always return NULL, and in 3ddac18 by only allowing stats:::weights.default if the argument is non-atomic (plain matrix is atomic).

@jarioksa
Copy link
Contributor

jarioksa commented Nov 8, 2024

@AlexaBennett : I do not have sufficient information of your particular case. I know how to get this error message (when you should not), but I do not know if this solves your problem. I do not even know what was your command that gave the error. However, if it was something like:

envfit(x ~ ., x.env)

Try replacing this with:

envfit(as.data.frame(x) ~ ., x.env)

If this fixes your problem, my planned fix will probably solve the same problem.

jarioksa added a commit that referenced this issue Nov 8, 2024
The following failed:
  mod <- rda(dune)
  envfit(scores(mod, display="sites") ~ ., dune.env)
    Error in object$weights : $ operator is invalid for atomic vectors
@AlexaBennett
Copy link
Author

@jarioksa Thank you for following up on this!

Try replacing this with:

envfit(as.data.frame(x) ~ ., x.env)

The function runs as expected (again). Here is a little added context about the arguments I used. The argument ord was a matrix with row names as sample names and columns as PC1..PCn. The argument env was a data frame with row names as sample names and columns containing either continuous or discrete variables.

jarioksa added a commit that referenced this issue Nov 10, 2024
The following failed:
  mod <- rda(dune)
  envfit(scores(mod, display="sites") ~ ., dune.env)
    Error in object$weights : $ operator is invalid for atomic vectors

(cherry picked from commit eb84ebd)
jarioksa added a commit that referenced this issue Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants