Re: [問題] 關於GAM的問題...
給你一個例子:
library(gam)
set.seed(100)
n = 32; p = 6
X = matrix(rnorm(n*p), n, p)
X2 = X
X2[,1] = sin(X[,1]/pi*2)
X2[,2] = 0.5*(X[,2])^2 - 1.2 * X[,2]
X2[,3] = 0.2*(X[,3])^3 - 0.5*(X[,3])^2
Beta = c(0.5,1,1,1,1,0,0)
Y = cbind(1, X) %*% Beta
dat = data.frame(X2, Y)
tmp = paste0(sapply(1:p, function(v) paste0("s(", names(dat)[v], ")")),
collapse = "+")
fm2 = as.formula( paste(names(dat)[(p+1)], tmp, sep = "~"))
summary(gam(fm2, data = dat)) # default of df = 3 => p = 3*6 = 18 < 32
df.v = c(6,6,6,6,6,6) # sum of df = 15 < n = 16
tmp = paste0(sapply(1:p, function(v) paste0("s(", names(dat)[v], ", df = ",
df.v[v], ")")), collapse = "+")
fm = as.formula( paste(names(dat)[(p+1)], tmp, sep = "~"))
summary(gam(fm, data = dat)) # run default k = 3 for all variables, p = 6 * 6
= 36 > n = 32, then the error is occurred.
df.v = c(6,6,6,1,1,1) # sum of df = 19 < n = 32
tmp = paste0(sapply(1:p, function(v) paste0("s(", names(dat)[v], ", df = ",
df.v[v], ")")), collapse = "+")
fm = as.formula( paste(names(dat)[(p+1)], tmp, sep = "~"))
summary(gam(fm, data = dat)) # no error.
df.v = c(6,6,6,1) # sum of df = 19 < n = 32
tmp = paste0(sapply(1:length(df.v), function(v) paste0("s(", names(dat)[v],
", df = ", df.v[v], ")")), collapse = "+")
tmp2 = paste0(names(dat)[(length(df.v)+1):p], collapse = "+")
fm = as.formula( paste(names(dat)[(p+1)], paste(tmp, tmp2, sep = "+"), sep =
"~"))
summary(gam(fm, data = dat)) # no error.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.238.86.11
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
0
7
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章