-
Notifications
You must be signed in to change notification settings - Fork 0
/
gam.sinus.R
88 lines (72 loc) · 2.47 KB
/
gam.sinus.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
library(magrittr)
library(splines)
f <- function(n = 1000, domain = c(0,10)){
x <- runif(n = n, min = domain[1], max = domain[2])
data.frame(
x = x,
y = (sin(x)+exp(-x)) |> jitter(factor = 1000)
)
}
data <- f()
data_train <- data[data$x <= 6,]
par(mfrow = c(2, 1))
plot(y ~ x, data = data_train)
plot(y ~ x, data = data)
par(mfrow = c(1, 1))
gam1 <- mgcv::gam(y ~ s(x, bs="tp"), data = data_train)
gam2 <- mgcv::gam(y ~ s(x, bs="cr"), data = data_train)
lm1 <- lm(y ~ bs(x, knots = 2), data = data_train)
mgcv::gam.check(gam1)
data2 <- data_train %>%
dplyr::mutate(
y_fit_gam1 = fitted(gam1),
y_fit_gam2 = fitted(gam2),
y_fit_lm = fitted(lm1)
)
ggplot2::ggplot(data = data2, ggplot2::aes(y = y, x = x)) +
ggplot2::geom_point() +
ggplot2::geom_point(ggplot2::aes(y = y_fit_gam2), color = "green") +
ggplot2::geom_point(ggplot2::aes(y = y_fit_gam1), color = "blue")
# ------------------------------------------------------------------------------
# Sinus
# ------------------------------------------------------------------------------
library(magrittr)
library(splines)
random_sin <- function(n = 1000, domain = c(0,2.5*pi)){
x <- runif(n = n, min = domain[1], max = domain[2])
data.frame(
x = x,
y = sin(x) |> jitter(factor = 1000)
)
}
data <- random_sin()
plot(y ~ x, data = data)
data_train <- data[data$x <= 2*pi,]
plot(y ~ x, data = data_train)
gam1 <- mgcv::gam(y ~ s(x, bs="tp"), data = data_train)
gam2 <- mgcv::gam(y ~ s(x, bs="cc"), data = data_train)
lm1 <- lm(y ~ bs(x, knots = 6), data = data_train)
data2 <- data_train %>%
dplyr::mutate(
y_fit_gam1 = fitted(gam1),
y_fit_gam2 = fitted(gam2),
y_fit_lm = fitted(lm1)
)
ggplot2::ggplot(data = data2, ggplot2::aes(y = y, x = x)) +
ggplot2::geom_point() +
ggplot2::geom_point(ggplot2::aes(y = y_fit_gam1), color = "blue") +
ggplot2::geom_point(ggplot2::aes(y = y_fit_gam1), color = "green") +
ggplot2::geom_point(ggplot2::aes(y = y_fit_lm), color = "red")
termplot(lm1, partial.resid = T, se=T)
plot(gam1)
data3 <- data %>%
dplyr::mutate(
y_fit_gam1 = predict(gam1, newdata = data),
y_fit_gam2 = predict(gam2, newdata = data),
y_fit_lm = predict(lm1, newdata = data)
)
ggplot2::ggplot(data = data3, ggplot2::aes(y = y, x = x)) +
ggplot2::geom_point() +
ggplot2::geom_point(ggplot2::aes(y = y_fit_gam1), color = "blue") +
ggplot2::geom_point(ggplot2::aes(y = y_fit_gam1), color = "green") +
ggplot2::geom_point(ggplot2::aes(y = y_fit_lm), color = "red")