BiologicalRecordsCentre / BRCindicators

An R package for creating indicators from trends data
4 stars 11 forks source link

Error in MSI #81

Closed Engine-Wu closed 3 months ago

Engine-Wu commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

Engine-Wu commented 2 years ago

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

Engine-Wu commented 2 years ago

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 commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

larspett commented 1 year ago

JAGS is 4.3.0

larspett commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@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: https://github.com/BiologicalRecordsCentre/BRCindicators/blob/31b7dc69a1fe88719e0c26f4a94a6de3f0dbfcbe/R/msi_tool.R#L141

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 commented 1 year ago

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

MichelleJudge commented 11 months ago

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 commented 8 months ago

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 commented 8 months ago

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

DylanCarbone commented 8 months ago

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.

larspett commented 8 months ago

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 GitHubhttps://github.com/BiologicalRecordsCentre/BRCindicators/issues/81#issuecomment-1853592016, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEVQXZHBFYZDZ6Y3UVATKVLYJF27RAVCNFSM53OQ3XZ2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TCOBVGM2TSMRQGE3A. You are receiving this because you were mentioned.Message ID: @.***>

AugustT commented 8 months ago

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

DylanCarbone commented 8 months ago

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 commented 8 months ago

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