510 questions
0
votes
1
answer
949
views
I want to display 2 smoothed variables from a GAMM on the same graph
x2 will represent factors "a" and "b" for this simplified example, and f3 is the random effect. (I don't know if the compound symmetry correlation is part of my problem).
gamm(...
0
votes
0
answers
250
views
GAMs in R: not working for first category of categorical variables
I'm using the mgcv package to create GAMs in R. Right now I am attempting to model the interaction between my numerical variable Depth and various categorical variables. I have converted all of the ...
0
votes
0
answers
593
views
"saturated likelihood may be inaccurate" warning and negative deviance when running betar family in GAM
My code when running the generalized additive model with the betar family is as follow.
libary(mgcv)
b1 <- gam(ssim_exp ~ s(stage, k = 4, fx = TRUE, by = comparison_type) + comparison_type, data = ...
0
votes
1
answer
251
views
GAM prediction in R with zero covariates
I have estimated a GAM model (using the mgcv package) with an intercept and two smoothed terms such as:
y = intercept + b1*s(x1) + b2*s(x2)
But when I predict y on new dataset of a single row with ...
-1
votes
2
answers
1k
views
Visualize Generalized Additive Model (GAM) in R [closed]
I want to achieve a GAM plot that looks like this
Image from https://stats.stackexchange.com/questions/179947/statistical-differences-between-two-hourly-patterns/446048#446048
How can I accomplish ...
0
votes
0
answers
161
views
Bootstrapping the uncertainty on an RMSE estimate of a location-scale generalized additive model
I have height data (numeric height data in cm; Height) of plants measured over time (numeric data expressed in days of the year; Doy). These data is grouped per genotype (factor data; Genotype) and ...
0
votes
1
answer
225
views
How to rename x-axis labels in gam plot in R?
I fitted the model using the following code
mod1 <- gam(severity ~ s(mean_rh, k = 8) + s(mean_temp, k = 10) + s(mean_ws, k =7) + s(avg_daily_rain, k = 7), family = betar(), data = dat_seasonal)
...
3
votes
2
answers
442
views
GAM distributed lag model with factor smooth interaction (by variable)
I'm trying to compare the climate response in the last 60 years of two subgroups of a plant (factor variable subgroups with 2 levels). The response of the two subgroups which both grew on the same ...
1
vote
0
answers
174
views
How can I debug BLAS/LAPACK error in mgcv::gam function
In R I'm running a mgcv::gam() function with 12000 observations and using 130 parameters (mostly factors and 3 splines. I get the following error message:
Error in magic(G$y, G$X, msp, G$S, G$off, L = ...
1
vote
1
answer
472
views
Computation time GAM
I am fitting the below GAM in mgcv
m3.2 <- bam(pt10 ~
s(year, by = org.type) +
s(year, by = region) +
s(org.name, bs = 're') +
s(org.name, year, bs = ...
0
votes
1
answer
415
views
Can I use poisson distribution as family in Generalized Additive Model (GAM) for continuous, non-negative data?
I am building a GAM with a data set which distribution resembles poisson-distributed data. However, my data is continuous, i.e., it contains information on tree volumes in cubic meters. So, when doing ...
0
votes
1
answer
262
views
Error when trying to fit Hierarchical GAMs (Model GS or S) using mgcv
I have a large dataset (~100k observations) of presence/absence data that I am trying to fit a Hierarchical GAM with individual effects that have a Shared penalty (e.g. 'S' in Pedersen et al. 2019). ...
0
votes
0
answers
76
views
Why won't gam.check run for my simple model? Error in xy.coords
I am trying to run different gam models using the mgcv package to predict animal behaviour based on time of day and latitude. My data frame has a 0/1 column that states whether an animal was detected ...
2
votes
1
answer
281
views
How to model quantiles regression curves for probabilities depending on a predictor in R?
I'd' like to model the 25th, 50th and 75th quantile regression curves (q25, q50, q75) for 241 values of probability ('prob') depending on x0.
For that purpose, I used the qgamV package as follows. ...
1
vote
1
answer
294
views
plotting non-transformed, interpretable output and raw data from GAM {mgcv} r
I am trying to understand the relationship between NDVI and elevation using a GAM {mgcv}.
ndvi=c(0.37284458, 0.36299109, 0.34534124, 0.35626486, 0.33304086,
0.34456021, 0.34147954, 0.37136942, 0....
0
votes
0
answers
88
views
Bug in derivative calculation GI type GAM
I have many hierarchical GAMs fitted with the brm() function of the brms package. Some of my GAMs have a 'I type' structure :
y ~ 1 + s(year, by = site, bs = "tp", m = 2) + s(level, bs = &...
0
votes
0
answers
161
views
How to use tensor function of gam model in train function of caret package in r?
I am using gam method with both spline and tensor interaction functions(s and ti) inside the train function (for test and train).
I know for spline functions in gam we can use method = "gam" ...
2
votes
1
answer
634
views
How can I apply interaction term with linear predictors in mgcv package
I have a question about using interaction term in mgcv package with 2 linear predictor.
I wrote the code to fit interaction between x1 and x2,
mgcv::gam(y ~ x1 + x2 + ti(x1, x2, k = 3), method = "...
0
votes
0
answers
221
views
How do I plot gam model response predictions on a (hemi)spherical heatmap in R?
I have built a model with a spherical smooth term containing separate smooths for each level of a factor (formulated as s(lat,lon, bs = "sos", by = factor)) using gam() in the mgcv package ...
2
votes
1
answer
602
views
Using gratia::fitted_values() to get response predictions for the global smooth in an HGAM
This question is a follow-up to this post: Using gratia::draw() in R to display partial effect plots within an HGAM that are not relative to the global smooth
I have a dataset that looks like this:
df ...
0
votes
1
answer
370
views
Calculate AUC and variables importance in mcgv::gam in R
Hello my dataset looks like this:
structure(list(pa = structure(c(2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, 1L, 2L, 1L, 1L), .Label = c("0", "1"), class = "factor"),
...
1
vote
1
answer
725
views
R: How is F statistic calculated in mgcv?
I have a simple GAM, with one smooth. When I run summary() or anova.gam() on the model, it gives me an F test for the significance of the smooth. However, it is unclear what dfs are being used to ...
0
votes
0
answers
91
views
Fixing parameter of random effect with gamm in R
Do you know if it is possible to fit a parameter of a random effect using gamm from the mgcv package?
For example a Normal distribution N(0, c) and fix c to a certain value instead of being estimated?
...
2
votes
1
answer
2k
views
Is there a way to include an autocorrelation structure in the gam function of mgcv?
I am building a model using the mgcv package in r. The data has serial measures (data collected during scans 15 minutes apart in time, but discontinuously, e.g. there might be 5 consecutive scans on ...
1
vote
1
answer
80
views
Having issues with or_gam function in oddsratio package
Data
Here is the dput of my data:
heart <- structure(list(died = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L,...
1
vote
0
answers
261
views
Qr component bug in mgcv::gam?
I tried to follow the example 7.2.6-Prediction with predict.gam
from Generalized Additive model : An introduction with R.
However, when I ran
predict(m2)[1:5]
It always gave the following error:
Error ...
1
vote
1
answer
562
views
GAM residuals missing in plot
I am applying a GAM model to my data: cell abundance over time.
The model works just fine (although I am aware of a pattern in my resiudals, but this is a different issue not relevant here).
It just ...
0
votes
0
answers
293
views
Derivative a interaction term in GAM Model
I'm trying to model an estimation of the price elasticity of demand for each customer using GAM model, a model like this:
\ln D = \ln P + \ln P \cdot \sum_{i=1}^{20} f(X_i)
PED = \frac{\partial \ln D ...
1
vote
1
answer
144
views
Using gratia::data_slice() for a GAM with an offset
I am trying to get my fitted values from a gam with a few of the features on the GitHub version of gratia and am having trouble using the data_slice() function with a model that has an offset. I am ...
2
votes
0
answers
148
views
Constraining the response scale of a smooth function in an mgcv GAM
tl/dr: I have a GAM of several predictor variables and smooth functions. I need to constrain one of these smooth functions such that the response of this function is between 0 and 1.
I have a GAM in ...
2
votes
0
answers
2k
views
Reporting mgcv::gam summary with modelsummary
I'm attempting to report the model summary from mgcv::gam() using the modelsummary package. The flextable package provides a summary that is consistent with the summary output in R and what is often ...
1
vote
1
answer
897
views
Using gratia::draw() in R to display partial effect plots within an HGAM that are not relative to the global smooth
I have a data set that looks like this:
df <- data.frame(
Lake = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, ...
3
votes
2
answers
1k
views
How to visualize GAM results with contour & tile plot (using ggplot2)
I would like to make a contour plot with ggplot2 by using gam results. Below is a detailed explanation of what I want:
#packages
library(mgcv)
library(ggplot2)
library(tidyr)
#prepare data
df <- ...
0
votes
2
answers
151
views
Limit maximum df of smooth in ggplot?
Firstly, I am very new to R, very basic statistical knowledge and have thus been winging it when it comes to my analysis. This means googling the coding I need for the results, and due to how small ...
3
votes
1
answer
648
views
mgcv: Difference between s(x, by=cat) and s(cat, bs='re')
What is the difference between adding a by= parameter to a smooth and adding a random effect smooth?
I've tried both, and get different results. E.g.:
library(mgcv)
set.seed(26)
gam.df <- tibble(y=...
0
votes
1
answer
236
views
Using an indicator function to estimate a smoothed effect when dummy variable is TRUE
I have often used indicator functions in my linear regression modelling to allow for the estimation of a coefficient only when a secondary covariate is TRUE. This is always in cases where it does not ...
1
vote
0
answers
125
views
Plotting a GAM contour plot with the response scale in tidymv
I have created a GAM using the mgcv package with the gamma family. I then plot it with tidymv so that I use ggplot2. However, it plots the transformed values for the scale. How do I plot it in the ...
2
votes
2
answers
1k
views
Space between axis title and tick labels in persp plot in R (using vis.gam from mgcv package)
I am making a perspective plot of my generalised additive model (GAM) named a1b, using vis.gam(), which in turn makes use of the persp function in R.
Code is as follows:
library(mgcv)
vis.gam(x = a1b, ...
2
votes
1
answer
718
views
mgcv random effects - extracting estimates and plots
I am using mgcv to fit GAMs with random effects, e.g.:
gam_fit <- gam(y ~ s(age) + s(region, bs='re'),
data = my_data,
method = 'REML')
See Gavin Simpson's ...
2
votes
1
answer
1k
views
Fitting a smooth spline (GAM function) in R: Error in number of knots required to fit spline -- knot requirements increasing
I'm trying to fit a smooth spline to what looks like data with two peaks. First, I fit a smooth spline to my data to identify the potential position of the knots.
library(npreg)
library(splines)
...
2
votes
1
answer
452
views
GAM error using bam() with family = betar
I'm having trouble solving an error I am getting when running bam() from mgcv.
I note that a similar error was reported here 14 months ago and there seemed to be no agreed on solution - with the ...
1
vote
0
answers
608
views
Predicting a GAM in R with an offset
I am trying to predict the output of two GAMs using the package mgcv and the predict() function and for some reason one output has the smooth prediction I am looking for and the other does not.
My ...
1
vote
1
answer
173
views
Annual count index from GAM looking at long-term trends by site
I'm interested in estimating a shared, global trend over time for counts monitored at several different sites using generalized additive models (gams). I've read this great introduction to ...
4
votes
0
answers
676
views
How to compare GAMs that include random effects and factors?
I am trying to make a model comparison (say, for hypothesis testing) of two GAMs (mgcv package),
where both models include random effects smooth term (s(bs="re")), and the
second model ...
1
vote
0
answers
524
views
Calculate conditional and marginal R2 from a gam/bam model
I have a generalised additive model calculated using the bam function from the mgcv package. I have two random effects in the model and 5 fixed effects, one of which is smoothed. The R2 are quite high ...
0
votes
1
answer
185
views
How to apply smooth parameter to main effect and interaction separately in mgcv?
I use the code below to apply smooth parameter to interaction term only.
mgcv::gam(Y ~ s(X1, k=3, sp=-1) + s(X2, k=4, sp=-1) +
ti(X1, X2, k=4, sp=c(1,1)) + X3,
method = "...
3
votes
1
answer
3k
views
Different estimates between bam and gam model (mgcv) and interaction term estimates edf of 0
I am new to fitting gamm models and ran into two problems with my analysis.
I ran the same model using the gam and the bam function of the package mgcv. The models give me different estimates, and I ...
2
votes
1
answer
1k
views
What does the y-axis "effect" mean after using gratia::draw for a GAM
I have made a GAM model using "mgcv" package with the family = inverse.guassian(link = identity) and I am really happy with the fit. After plotting the smooth terms using gratia:draw(GAM, ...
1
vote
1
answer
177
views
R) Why te() has a "cr" for default basis option in mgcv package?
In the mgcv package, s() term has a bs option with "tp" as a default value.
As I understand, "tp" is a optimal smoother for any dimension or rank, so it is used for default.
But in ...
2
votes
1
answer
345
views
Using gratia::smooth_estimates() with a tensor to produce a heatmap for a defined area (e.g., US state lines) in R
I have a data set that looks like this:
structure(list(count = c(85970L, 57321L, 46409L, 30436L, 47316L,
2037L, 82188L, 127510L, 97109L, 15328L, 37766L, 74320L, 197130L,
73258L, 27795L, 52434L, ...