USEPA / SSN2

SSN2: Spatial Modeling on Stream Networks in R
https://usepa.github.io/SSN2/
GNU General Public License v3.0
13 stars 3 forks source link

Inf and Nan in pseduo R2 and residuals using beta family glm #23

Closed katiewampler closed 2 months ago

katiewampler commented 2 months ago

I'm using the beta family glm SSN models, and for some (but not all) models, when I get the model statistics the pseduo R2 and residuals are missing/Nan/Inf/NA.

I was able to track down the code causing the issue: in the get_deviance_glm function, the gamma function was returning Inf values when above ~171. Some of my values where above this and returning Inf values.

It appeared using a modified function: constant <- log(gamma(fitted_response dispersion)) +
as.numeric(log(Rmpfr::igamma(((1 - fitted_response)
dispersion), 0))) - log(gamma(y dispersion)) - as.numeric(log(Rmpfr::igamma(a=((1 - y) dispersion),x=0))) fixed the issue.

Here's the code I used which produced the error: library(SSN2) library(sf) library(dplyr)

ssn <- ssn_import("~/1_Research/5_McKenzie_EEMs/data/parafac_ssn/lsn.ssn") #load SSN df<- ssn_get_data(ssn) df[,6:11] <- sapply(df[,6:11] %>%st_drop_geometry(), function(x){x/100}) #convert to a fraction ssn <- ssn_put_data(df, ssn) #put converted data in model mod <- ssn_glm(Comp_1 ~ DNBR + Season, ssn, family="beta", tailup_type = "none", taildown_type = "none", euclid_type = "none", additive = "afvArea", random=~Site) summary(mod) lsn.ssn.zip

michaeldumelle commented 2 months ago

Thanks @katiewampler ! I identified the numerical problem (with help from you) and implemented a stability update (linked here).

The code is working as intended now on the development branch of SSN2, which you can download by running

remotes::install_github("USEPA/SSN2", ref = "develop")

This update will be part of the next CRAN release (the current version on CRAN is 0.2.0), which should up before the end of the year.

It's worth noting that I also implemented this stability update for spmodel.