Skip to contents

Fits a Poisson GLM model y ~ x with the numeric response y and the numeric predictor x using stats::glm() and returns the R-squared of the observations against the predictions (see score_r2()).

Supports cross-validation via the arguments arguments cv_training_fraction (numeric between 0 and 1) and cv_iterations (integer between 1 and n) introduced via ellipsis (...). See preference_order() for further details.

Usage

f_count_glm(df, ...)

Arguments

df

(required, dataframe) with columns:

  • "x": (numeric, character, factor) predictor.

  • "y" (integer) counts response.

...

(optional) Accepts the arguments cv_training_fraction (numeric between 0 and 1) and cv_iterations (integer between 1 and Inf) for cross validation.

Value

numeric or numeric vector: R-squared

Examples


data(vi_smol, package = "spatialData")

df <- data.frame(
  y = vi_smol[["vi_counts"]],
  x = vi_smol[["swi_max"]]
)

#no cross-validation
f_count_glm(df = df)
#> [1] 0.4922978

#cross-validation
f_count_glm(
  df = df,
  cv_training_fraction = 0.5,
  cv_iterations = 10
  )
#>  [1] 0.5334181 0.4828414 0.5228786 0.4353532 0.5318422 0.4599100 0.5121896
#>  [8] 0.5046470 0.5069517 0.4548940

#categorical predictor
df <- data.frame(
  y = vi_smol[["vi_counts"]],
  x = vi_smol[["koppen_zone"]]
)

f_count_glm(df = df)
#> [1] 0.8182549