Re: [問題] 關於GAM的問題...

看板R_Language作者 (攸藍)時間10年前 (2014/03/15 22:25), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
給你一個例子: 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
文章代碼(AID): #1J96BKIt (R_Language)
文章代碼(AID): #1J96BKIt (R_Language)