Skip to content

Instantly share code, notes, and snippets.

@mathesong
Created February 19, 2019 11:24
Show Gist options
  • Save mathesong/7db5e59ffadb74e25d5d3449484ae751 to your computer and use it in GitHub Desktop.
Save mathesong/7db5e59ffadb74e25d5d3449484ae751 to your computer and use it in GitHub Desktop.
I cannot predict more than 1000 points using the mgcv package at a time, otherwise it starts repeating the values
library(mgcv)
library(tidyverse)
# Data
BPR <-
tibble::tribble(
~time, ~bpr,
50, 0.892042350231095,
170, 0.888132434012036,
230, 0.87170777383751,
290, 0.838972349740963,
350, 0.930837665079573,
410, 0.831346095761359,
470, 0.947754901271871,
530, 0.763998744037918,
620, 0.834635757258697,
1190, 0.70956560704307,
1790, 0.551889549531392,
2390, 0.697936911346988,
3041, 0.658171861836563,
3590, 0.656165891852214,
4190, 0.687996980066,
4790, 0.654859498649481,
5390, 0.627913384922731
)
# Fit
bprspline <- mgcv::gam(bpr ~ splines::bs(time),
data=BPR)
# Predict - for given values
BPR$splinepred <- predict(bprspline)
ggplot(BPR, aes(x=time, y=bpr)) +
geom_point() +
geom_line(aes(y=splinepred))
# Predict - for 500 interpolated values
interptime <- seq(50, 5390, length.out=500)
splinepred_df <- tibble::tibble(time = interptime,
splinepred = predict(bprspline,
newdata=data.frame(
time=interptime)))
ggplot(BPR, aes(x=time, y=bpr)) +
geom_point() +
geom_line(data=splinepred_df, aes(x=time, y=splinepred))
# Predict - for 1000 interpolated values
interptime <- seq(50, 5390, length.out=1000)
splinepred_df <- tibble::tibble(time = interptime,
splinepred = predict(bprspline,
newdata=data.frame(
time=interptime)))
ggplot(BPR, aes(x=time, y=bpr)) +
geom_point() +
geom_line(data=splinepred_df, aes(x=time, y=splinepred))
# Predict - for 4000 interpolated values
interptime <- seq(50, 5390, length.out=4000)
splinepred_df <- tibble::tibble(time = interptime,
splinepred = predict(bprspline,
newdata=data.frame(
time=interptime)))
ggplot(BPR, aes(x=time, y=bpr)) +
geom_point() +
geom_line(data=splinepred_df, aes(x=time, y=splinepred))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment