-
Notifications
You must be signed in to change notification settings - Fork 11
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
Error in MSI #81
Comments
I'm not able to reproduce this issue. The below code works for me. Are you able to share a reproducible example? devtools::install_github('biologicalrecordscentre/brcindicators')
library(BRCindicators)
set.seed(123)
# Create some example data in the format required
nyr = 20
species = rep(letters, each = nyr)
year = rev(rep(1:nyr, length(letters)))
# Create an index value that increases with time
index = rep(seq(50, 100, length.out = nyr), length(letters))
# Add randomness to species
index = index * runif(n = length(index), 0.7, 1.3)
# Add correlated randomness acrosss species, to years
index = index * rep(runif(0.8, 1.2, n = nyr), length(letters))
se = runif(n = nyr * length(letters), min = 10, max = 20)
data <- data.frame(species, year, index, se)
# Our species are decreasing
plot(data$year, data$index)
# Species index values need to be 100 in the base year. Here I use
# the first year as my base year and rescale to 100. The standard error
# in the base year should be 0.
min_year <- min(data$year)
for(sp in unique(data$species)){
subset_data <- data[data$species == sp, ]
multi_factor <- 100 / subset_data$index[subset_data$year == min_year]
data$index[data$species == sp] <- data$index[data$species == sp] * multi_factor
data$se[data$species == sp] <- data$se[data$species == sp] * multi_factor
data$se[data$species == sp][1] <- 0
}
# Run the MSI function
msi_out <- msi(data, plot = FALSE)
# Plot the resulting indicator
plot(msi_out) |
Your error suggests that somewhere R is trying to |
I copy your code and run , but receive the same error information. |
I just run the example code for MSI in the help in the help documentation. Here are the example code. Create an index value that increases with timeindex = rep(seq(50, 100, length.out = nyr), length(letters)) Add randomness to speciesindex = index * runif(n = length(index), 0.7, 1.3) Add correlated randomness acrosss species, to yearsindex = index * rep(runif(0.8, 1.2, n = nyr), length(letters)) se = runif(n = nyr * length(letters), min = 10, max = 20) data <- data.frame(species, year, index, se) Our species are decreasingplot(data$year, data$index) Species index values need to be 100 in the base year. Here I usethe first year as my base year and rescale to 100. The standard errorin the base year should be 0.min_year <- min(data$year) for(sp in unique(data$species)){ subset_data <- data[data$species == sp, ] } Run the MSI functionmsi_out <- msi(data, plot = FALSE) Plot the resulting indicatorplot(msi_out)` |
I test the package. The result is below. "Error in `==> devtools::test() ℹ Loading BRCindicators
Error (testbma.R:84:3): degraded data
Error (testbma.R:112:3): model options
Error (testbma.R:175:3): different parameters
══ Results ═════════════════════════════════════════════════════════════════════════════════════ [ FAIL 5 | WARN 0 | SKIP 0 | PASS 9 ]` |
I too have explored this and I do not get the error. Perhaps it is a versioning issue. Would you please send us your |
Here it is: R version 4.2.1 (2022-06-23 ucrt) Matrix products: default locale: attached base packages: other attached packages: loaded via a namespace (and not attached): |
I ran the example code for msi and keep getting
|
And here is the code snippet (from the help) that produced that error on my system |
JAGS is 4.3.0 |
Could it be that either a required library was not brought over to the new directory during a main R upgrade or that the JAGS (or something else that is required) has an incorrect path in R after an upgrade? BRCindicators worked quite recently on this win10 computer but since then R had a major upgrade and I ported the libraries to the new directory. |
Ping @AugustT @drnickisaac The demo code runs just fine on R i386 4.1.3 but not on R 4.2.1 on my machine. Can't test i386 4.1.3 on RStudio since RStudio crashes then (not happy with i386 when 4.2.1 x64 is installed) |
@larspett Thanks for pining down the cause. I'm travelling for the next 2 weeks so it is unlikely I will have the time to dig into it @drnickisaac The error is from here: Line 141 in 31b7dc6
This function should be debugged and the values in Looking at the R change logs has not given me any greater insight, but it does look like they made a lot of changes to the Windows version on this release. I think start with tracing back index through the function. |
@AugustT @drnickisaac @drnickisaac I run the demo code in MAC and Linux system. I get the same error information. |
Hi, I am wondering if there was a workaround for this issue? I tried today using R4.2.2 and I receiving the same error as mentioned above: Error: "Error in log(index): non-numeric argument to mathematical function" is in the MSI. Any advice would be appreciated. |
Hi, did you get this working @AugustT @drnickisaac ? It works on my OSX machines but still fails on the Win10 machine JAGS. Tried installing from the repo now and running the example code but still get the same error |
@DylanCarbone can you have a look into this and we can discuss at out next meeting? |
Hi Tom and Lars, The error occurs because of the step that Tom flagged:
It attempts to log transform index, which is already a vector, but fails because the vector contains element names. Using the base R function, unlist() with parameter use.names set to FALSE fixes this log transformation step. The function then incorrectly indexes the log transformed vector, treating it as if it was a matrix. Therefore, to fix this error, change the following lines: Lines 133 to 151 in 31b7dc6
To this:
I'm unsure why some users are able to run the original function whilst others are not. There must have been some changes to the as.vector() function that affects the indexing of named vectors, but I cannot find articles on this. |
Ok, will you update the repo with this?
13 dec. 2023 kl. 10:50 skrev DylanCarbone ***@***.***>:
Hi Tom and Lars,
The error occurs because of the step that Tom flagged:
LNindex <- as.vector(log(index))
It attempts to log transform index, which is already a vector, but fails because the vector contains element names. Using the base R function, unlist() with parameter use.names set to FALSE fixes this log transformation step.
The function then incorrectly indexes the log transformed vector, treating it as if it was a matrix.
Therefore, to fix this error, change the following lines:
https://github.com/BiologicalRecordsCentre/BRCindicators/blob/31b7dc69a1fe88719e0c26f4a94a6de3f0dbfcbe/R/msi_tool.R#L133-L151
To this:
# reset parameters
index <- unlist(INP5["index"], use.names = F)
se <- unlist(INP5["se"], use.names = F)
nobs <- NROW(INP5)
uspecies <- sort(unique(INP5$species))
nspecies <- length(uspecies)
year <- rep(uyear, nspecies)
# Transform indices and standard deviations to log scale (Delta method)
LNindex <- log(index)
LNse <- se/index
# Monte Carlo simulations of species indices
MC <- matrix(NA, nobs, nsim)
for (s in 1:nsim) {
for (o in 1:nobs) {
MC[o,s] <- rnorm(1, LNindex[o], LNse[o])
}
}
MC[MC < log(TRUNC)] <- log(TRUNC)
I'm unsure why some users are able to run the original function whilst others are not. There must have been some changes to the as.vector() function that affects the indexing of named vectors, but I cannot find articles on this.
—
Reply to this email directly, view it on GitHub<#81 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AEVQXZHBFYZDZ6Y3UVATKVLYJF27RAVCNFSM53OQ3XZ2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBVGM2TSMRQGE3A>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@larspett of course. @DylanCarbone is learning how to manage R packages and he and I will run through this at our next meeting |
Hi @larspett, we have just updated the repo. When you have the time, please reinstall the package and let us know if you encounter any further issues. Thanks |
Hi @DylanCarbone I did right away and it works just fine! Many thanks! |
I run the example code and finally received the error information like that “Error in log(index): non-numeric argument to mathematical function”. I don't know how to fix this problem. @AugustT
The text was updated successfully, but these errors were encountered: