I have a dataframe with 365814 observations that I want to use for fitting a GAMM with mgcv. The following code works:
Speed_model_test <- bam(mean_dist~ habitat_type2 +
s(Forest_density, bs="cr") +
s(Ruggedness, bs="cr") +
Hunting_in_season +
Winter_enclosure_yes_no +
sex,
data = red_deer_15_min2,
link = gaussian(type="log"))
However, as soon as I add a random effect for the different individuals in the dataset, I get an error when fitting the model with the same dataframe:
Speed_model_test <- bam(mean_dist~ habitat_type2 +
s(Forest_density, bs="cr") +
s(Ruggedness, bs="cr") +
Hunting_in_season +
Winter_enclosure_yes_no +
sex +
s(individual.local.identifier, bs="re"),
data = red_deer_15_min2,
link = gaussian(type="log"))
Error in model.matrix.formula(form, data) : data must be a data.frame
dput of a subset of 20 rows of the data frame;
structure(list(individual.local.identifier = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("287", "288"), class = "factor"),
sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("f",
"m"), class = "factor"), Winter_enclosure_yes_no = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Enclosure", "Not_in_enclosure"
), class = "factor"), habitat_type2 = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, 1L), .Label = c("Forest broadleaved", "Forest mixed"
), class = "factor"), Forest_density = c(89, 89, 89, 85,
89, 78, 89, 78, 78, 85, 82, 88, 92, 92, 84, 79, 81, 82, 78,
91, 91), Elevation = c(812.71819639231, 812.71819639231,
812.71819639231, 814.688653270534, 812.71819639231, 810.163842873485,
812.71819639231, 810.863673487378, 810.863673487378, 819.792779240948,
875.486756577389, 875.486756577389, 871.090551232063, 870.898053814855,
867.046446166005, 884.955026288904, 892.296706081018, 891.967052125287,
894.643926600538, 891.967052125287, 891.967052125287), Ruggedness = c(1.70343459848473,
1.70343459848473, 1.70343459848473, 1.55689594664619, 1.70343459848473,
1.82170429946155, 1.70343459848473, 2.83758431946717, 2.83758431946717,
3.09308072656422, 3.5744549073835, 3.5744549073835, 3.63864413714904,
3.42917788317389, 3.68377637029529, 3.30551491495578, 2.60185283715349,
2.77726377967515, 2.53177892113446, 2.77726377967515, 2.77726377967515
), Hunting_in_season = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("no", "yes"), class = "factor"), Day_of_year = c(67,
67, 67, 67, 67, 67, 67, 67, 67, 67, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19), mean_dist = c(11.5347165418407, 6.54004030266076,
13.286684066724, 21.4957340806599, 32.7032985028738, 39.4752350759861,
141.139924385665, 123.388962245111, 23.9520420806201, 49.7366465519833,
12.221956345304, 29.4937937417243, 23.1460769378941, 13.9594085803245,
69.5344965200506, 109.013360897755, 69.5396750161732, 31.5522698537152,
24.0577391677004, 15.2614245150277, 17.847702176831)), row.names = c(2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 3145L, 3146L, 3147L, 3148L,
3149L, 3150L, 3151L, 3152L, 3153L, 3154L, 3155L), class = "data.frame")
What could cause this error and how can it be solved?
Thanks a lot in advance!
individual.local.identifier
a factor in the data frame? – Gavin Simpsongam()
, just to try? – Gavin Simpson