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

Error in MSI #81

Closed
Engine-Wu opened this issue Jul 13, 2022 · 22 comments
Closed

Error in MSI #81

Engine-Wu opened this issue Jul 13, 2022 · 22 comments

Comments

@Engine-Wu
Copy link

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
MessageCenterUI_o1yX1thF6s

@AugustT
Copy link
Member

AugustT commented Jul 13, 2022

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)

@AugustT
Copy link
Member

AugustT commented Jul 13, 2022

Your error suggests that somewhere R is trying to log something like a NULL or logical

@Engine-Wu
Copy link
Author

I copy your code and run , but receive the same error information.

@Engine-Wu
Copy link
Author

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)

I just run the example code for MSI in the help in the help documentation. Here are the example code.
`# 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)`

@Engine-Wu
Copy link
Author

I test the package. The result is below. "Error in log(index): non-numeric argument to mathematical function" is in the MSI.

`==> devtools::test()

ℹ Loading BRCindicators
ℹ Testing BRCindicators
✔ | F W S OK | Context
✖ | 4 0 | Test bma [0.3s]
────────────────────────────────────────────────────────────────────────────────────────────────
Error (testbma.R:16:3): simple run
Error: Package 'jagsUI' is needed for the 'bma' function to work. Please insatll this from CRAN. You will also be required to install JAGS, which you can download from https://sourceforge.net/projects/mcmc-jags/files/JAGS/
Backtrace:

  1. BRCindicators::bma(...)
    at testbma.R:16:2

Error (testbma.R:84:3): degraded data
Error: Package 'jagsUI' is needed for the 'bma' function to work. Please insatll this from CRAN. You will also be required to install JAGS, which you can download from https://sourceforge.net/projects/mcmc-jags/files/JAGS/
Backtrace:

  1. BRCindicators::bma(...)
    at testbma.R:84:2

Error (testbma.R:112:3): model options
Error: Package 'jagsUI' is needed for the 'bma' function to work. Please insatll this from CRAN. You will also be required to install JAGS, which you can download from https://sourceforge.net/projects/mcmc-jags/files/JAGS/
Backtrace:

  1. BRCindicators::bma(...)
    at testbma.R:112:2

Error (testbma.R:175:3): different parameters
Error: Package 'jagsUI' is needed for the 'bma' function to work. Please insatll this from CRAN. You will also be required to install JAGS, which you can download from https://sourceforge.net/projects/mcmc-jags/files/JAGS/
Backtrace:

  1. BRCindicators::bma(...)
    at testbma.R:175:2
    ────────────────────────────────────────────────────────────────────────────────────────────────
    ✔ | 2 | Test lambda_indicator [7.6s]
    ✖ | 1 0 | Test msi
    ────────────────────────────────────────────────────────────────────────────────────────────────
    Error (testmsi.R:39:3): runs without error
    Error in log(index): non-numeric argument to mathematical function
    Backtrace:
  2. BRCindicators::msi(data, plot = FALSE)
    at testmsi.R:39:2
  3. BRCindicators::msi_tool(...)
    at BRCindicators-master/R/msi.R:102:2
  4. base::as.vector(log(index))
    at BRCindicators-master/R/msi_tool.R:141:2
    ────────────────────────────────────────────────────────────────────────────────────────────────
    ✔ | 1 | Test simulate_indicator
    ✔ | 6 | Test trend_summary_helper_functions

══ Results ═════════════════════════════════════════════════════════════════════════════════════
Duration: 8.0 s

[ FAIL 5 | WARN 0 | SKIP 0 | PASS 9 ]`

@drnickisaac
Copy link
Contributor

I too have explored this and I do not get the error. Perhaps it is a versioning issue. Would you please send us your sessionInfo()

@larspett
Copy link

Here it is: R version 4.2.1 (2022-06-23 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Swedish_Sweden.utf8 LC_CTYPE=Swedish_Sweden.utf8 LC_MONETARY=Swedish_Sweden.utf8
[4] LC_NUMERIC=C LC_TIME=Swedish_Sweden.utf8

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

other attached packages:
[1] BRCindicators_1.3.7

loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 plyr_1.8.7 pillar_1.8.1 compiler_4.2.1 later_1.3.0
[6] RColorBrewer_1.1-3 tools_4.2.1 boot_1.3-28 lifecycle_1.0.3 tibble_3.1.8
[11] gtable_0.3.1 nlme_3.1-157 lattice_0.20-45 mgcv_1.8-40 pkgconfig_2.0.3
[16] rlang_1.0.6 Matrix_1.5-1 DBI_1.1.3 cli_3.4.1 rstudioapi_0.14
[21] RPostgreSQL_0.7-4 coda_0.19-4 stringr_1.4.1 dplyr_1.0.10 generics_0.1.3
[26] vctrs_0.4.2 grid_4.2.1 tidyselect_1.2.0 glue_1.6.2 R6_2.5.1
[31] fansi_1.0.3 pool_0.1.6 carData_3.0-5 ggplot2_3.3.6 reshape2_1.4.4
[36] car_3.1-0 magrittr_2.0.3 scales_1.2.1 splines_4.2.1 abind_1.4-5
[41] assertthat_0.2.1 colorspace_2.0-3 utf8_1.2.2 stringi_1.7.8 munsell_0.5.0

@larspett
Copy link

I ran the example code for msi and keep getting

Run the MSI function

msi_out <- msi(data, plot = FALSE)
Error in log(index) : non-numeric argument to mathematical function

@larspett
Copy link

And here is the code snippet (from the help) that produced that error on my system
msi.txt

@larspett
Copy link

JAGS is 4.3.0

@larspett
Copy link

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.

@larspett
Copy link

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)

@AugustT
Copy link
Member

AugustT commented Oct 17, 2022

@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:

LNindex <- as.vector(log(index))

This function should be debugged and the values in index need to be checked throughout the function. I suspect a change to something in base R somewhere is changing some behaviour that results in a logical NA or NULL appearing in the index vector. Quite where the behaviour change has come from I can't say, but it could be as fa back as reading in the csv right at the beginning.

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.

@Engine-Wu
Copy link
Author

@AugustT @drnickisaac @drnickisaac I run the demo code in MAC and Linux system. I get the same error information.

@MichelleJudge
Copy link

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.
Error occurs after running the line: LNindex <- as.vector(log(index))

Any advice would be appreciated.
Thanks!

@larspett
Copy link

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

@AugustT
Copy link
Member

AugustT commented Dec 11, 2023

@DylanCarbone can you have a look into this and we can discuss at out next meeting?

@DylanCarbone
Copy link
Collaborator

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:

BRCindicators/R/msi_tool.R

Lines 133 to 151 in 31b7dc6

index <- as.vector(INP5["index"])
se <- as.vector(INP5["se"])
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 <- as.vector(log(index))
LNse <- as.vector(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,1], LNse[o,1])
}
}
MC[MC < log(TRUNC)] <- log(TRUNC)

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.

@larspett
Copy link

larspett commented Dec 13, 2023 via email

@AugustT
Copy link
Member

AugustT commented Dec 13, 2023

@larspett of course. @DylanCarbone is learning how to manage R packages and he and I will run through this at our next meeting

@DylanCarbone
Copy link
Collaborator

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

@larspett
Copy link

Hi @DylanCarbone I did right away and it works just fine! Many thanks!

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

6 participants