Skip to contents

Fits a Gaussian GAM model y ~ s(x) (y ~ x if x is non-numeric) with the numeric response y and the numeric, character or factor predictor x using mgcv::gam() 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_numeric_gam(df, ...)

Arguments

df

(required, dataframe) with columns:

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

  • y (numeric) continuous 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_numeric"]],
  x = vi_smol[["swi_max"]]
)

#no cross-validation
f_numeric_gam(df = df)
#> [1] 0.6306029

#cross-validation
f_numeric_gam(
  df = df,
  cv_training_fraction = 0.5,
  cv_iterations = 10
  )
#>  [1] 0.6187690 0.6060670 0.6206664 0.6236273 0.5858357 0.6490390 0.5872209
#>  [8] 0.5788600 0.6038059 0.6336038

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

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