【问题标题】:"gam" model of caret not returning fitted.values插入符号的“gam”模型不返回fitted.values
【发布时间】:2013-04-03 15:56:50
【问题描述】:

我在caret.train 中使用gam 模型(caret 使用包mgcv 中的gam):

> fit <- train(P~II+TH+DR+TT,data=training,method="gam",trControl=ctrl,metric="Rsquared",preProc=c("center","scale"))
> names(fit)
 [1] "method"       "modelType"    "results"      "pred"         "bestTune"     "call"        
 [7] "dots"         "metric"       "control"      "finalModel"   "preProcess"   "trainingData"
 [13] "resample"     "resampledCM"  "perfNames"    "maximize"     "yLimits"      "times"       
 [19] "terms"        "coefnames"    "xlevels" 

我没有看到上面的fitted.values,但是gam 对象应该返回fitted.values - http://hosho.ees.hokudai.ac.jp/~kubo/Rdoc/library/gam/html/gam.html 结果,我无法绘制拟合残差图,并且其他一些功能也失败了。一种解决方法是直接使用gam 而不是caret,但我也打算使用其他模型,并且想要一个一致的界面。

请指教。

编辑:

  1. 数据快照 - dput(head(training)) 输出:

    structure(list(TT = c(1.810376, 0.089206, 0.623906, 0.676775, 
    0.206524, 1.014849), P = c(682L, 674L, 681L, 679L, 655L, 682L
    ), II = c(846000000L, 4790000L, 38600000L, 40600000L, 1379632L, 
    7526080L), WSM = c(5272L, 144L, 576L, 576L, 2336L, 18696L), TSM = c(168704L, 
    4608L, 18432L, 18432L, 74752L, 598272L), L2M = c(1.49e+09, 12600000, 
    85700000, 1.24e+08, 4214560, 33560200), DR = c(2.52e+09, 18400000, 
    1.3e+08, 1.8e+08, 5559030, 44681000), DW = c(11600000L, 5440000L, 
    39600000L, 46400000L, 4920550L, 36812430L), TH = c(32.032843125, 
    0.1880727305, 0.2003506939, 0.1983195715, 0.558498625, 0.495952125
    )), .Names = c("TT", "P", "II", "WSM", "TSM", "L2M", "DR", "DW", 
    "TH"), row.names = c(3L, 5L, 7L, 8L, 9L, 10L), class = "data.frame")
    
  2. str(fit) 按照@nograpes 的建议在finalModel 中显示fitted.values

    $ finalModel  :List of 50
    ..$ coefficients     : Named num [1:37] 761 -1839 -377 745 -473 ...
    .. ..- attr(*, "names")= chr [1:37] "(Intercept)" "s(II).1" "s(II).2" "s(II).3" ...
    ..$ residuals        : num [1:44] -8.229 0.402 -11.41 -26.357 -8.202 ...
    ..$ fitted.values    : Named num [1:44] 690 674 683 707 687 ...
    

【问题讨论】:

  • 上周没有提出非常相似的问题吗?
  • 你指的是这个吗? - stackoverflow.com/questions/15584541/…
  • 没有。我指的是您之前提出的两个问题:stackoverflow.com/questions/15724807/…stackoverflow.com/questions/15745300/… 投票结束。由于没有数据,也没有针对较早的问题发布解决方案,这不太可能产生成效。你应该联系包维护者。
  • 好的,不...上一个问题与我在调用train 函数时遇到的错误有关。这是因为我同时加载了mgcvcaret 使用mgcv 无论如何gam)和gam 库,当我分离gam 库时,它起作用了。
  • 您应该发布已解决问题的后续答案(并删除重复的问题)。这样他们就会被标记为已回答,您的贡献将帮助面临相同问题的其他人(如果他们搜索)。

标签: r regression gam mgcv


【解决方案1】:

如果您提供了一些示例数据,这个问题会更容易回答。

检查对象的names 并不是找出其中内容的好方法。尝试运行str(fit),你会看到你缺少什么。

fitted.values 对象嵌套更深一层。

library(mgcv)
library(caret)

dat <- gamSim(1,n=400,dist="normal",scale=2)
test<-train(y~x1, data=dat, method='gam') # Next time, provide any data like this.

test$finalModel$fitted.values # If that isn't what you want, try str(test)

【讨论】:

    【解决方案2】:

    http://caret.r-forge.r-project.org/modelList.html

    使用 method = "gam" 可以从 mgcv 包中获取 gam(),使用 "gamLoess" 和 "gamSpline" 可以使用 gam 包中的 gam()。

    > library(gam)
    > data(kyphosis)
    > mod <- train(Kyphosis ~ Age + Number, data = kyphosis, method = "gamSpline")
    > mod$finalModel
    Call:
    gam:::gam(formula = smootherFormula(data[, !(names(data) %in% 
        ".outcome"), drop = FALSE], smoother = "s", df = tuneValue$.df), 
        family = if (type == "Regression") gaussian() else binomial(), 
        data = data)
    
    Degrees of Freedom: 80 total; 76.99992 Residual
    Residual Deviance: 64.9097 
    > head(mod$finalModel$fitted.values)
             1          2          3          4          5          6 
    0.15217276 0.10961985 0.27168469 0.07017040 0.04072463 0.01414652 
    

    最大

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-12-17
      • 2018-07-24
      • 2019-03-25
      相关资源
      最近更新 更多