全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
6425 10
2017-05-03


b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),data=dat)
拟合一个模型后想用write.csv(b,"b.csv"),
出现下列错误
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :

  cannot coerce class "c("gam", "glm", "lm")" to a data.frame
如何把b读到csv文档里面,不想用summary(),想用write()


求高手指点。谢谢

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2017-5-3 20:39:59
b是一个模型,不是data frame
而write.csv一般是默认数据框格式的数据。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-5-3 21:58:31
net_test 发表于 2017-5-3 20:39
b是一个模型,不是data frame
而write.csv一般是默认数据框格式的数据。
有什么好办法吗
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-5-4 19:55:42
顶一下
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-5-4 22:35:42
str(b)查看b的结构
对b中的内容,分别存储,你需要什么才存储什么。一般存储csv都是矩阵或者data.frame
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2017-5-5 14:26:27
colinxf 发表于 2017-5-4 22:35
str(b)查看b的结构
对b中的内容,分别存储,你需要什么才存储什么。一般存储csv都是矩阵或者data.frame
List of 47
$ coefficients     : Named num [1:37] 7.833 0.217 0.381 -0.136 -0.377 ...
  ..- attr(*, "names")= chr [1:37] "(Intercept)" "s(x0).1" "s(x0).2" "s(x0).3" ...
$ residuals        : num [1:400] -1.676 -4.662 0.957 -0.574 0.525 ...
$ fitted.values    : num [1:400] 8.8 7.63 3.02 11.01 14.05 ...
$ family           :List of 11
  ..$ family    : chr "gaussian"
  ..$ link      : chr "identity"
  ..$ linkfun   :function (mu)  
  ..$ linkinv   :function (eta)  
  ..$ variance  :function (mu)  
  ..$ dev.resids:function (y, mu, wt)  
  ..$ aic       :function (y, n, mu, wt, dev)  
  ..$ mu.eta    :function (eta)  
  ..$ initialize:  expression({     n <- rep.int(1, nobs)     if (family$link == "inverse")          mustart <- y + (y == 0) * sd(y) * 0.01     else if (family$link == "log")          mustart <- pmax(y, 0.01 * sd(y))     else mustart <- y })
  ..$ validmu   :function (mu)  
  ..$ valideta  :function (eta)  
  ..- attr(*, "class")= chr "family"
$ linear.predictors: num [1:400] 8.8 7.63 3.02 11.01 14.05 ...
$ deviance         : num 1504
$ null.deviance    : num 5469
$ iter             : int 1
$ weights          : num [1:400] 1 1 1 1 1 1 1 1 1 1 ...
$ prior.weights    : num [1:400] 1 1 1 1 1 1 1 1 1 1 ...
$ df.null          : int 399
$ y                : num [1:400] 7.13 2.97 3.98 10.43 14.57 ...
$ converged        : logi TRUE
$ sig2             : num 3.9
$ edf              : Named num [1:37] 1 0.4109 0.0519 0.0547 -0.1747 ...
  ..- attr(*, "names")= chr [1:37] "(Intercept)" "s(x0).1" "s(x0).2" "s(x0).3" ...
$ edf1             : Named num [1:37] 1 0.655 0.213 0.109 -0.207 ...
  ..- attr(*, "names")= chr [1:37] "(Intercept)" "s(x0).1" "s(x0).2" "s(x0).3" ...
$ hat              : num [1:400] 0.03 0.0259 0.0845 0.0383 0.048 ...
$ F                : num [1:37, 1:37] 1.00 6.71e-11 2.09e-11 -1.87e-11 -7.77e-12 ...
$ R                : num [1:37, 1:37] 6.66e-16 -2.00e+01 0.00 0.00 0.00 ...
$ boundary         : logi FALSE
$ sp               : Named num [1:4] 1.37e-01 1.66e-01 4.71e-04 1.97e+09
  ..- attr(*, "names")= chr [1:4] "s(x0)" "s(x1)" "s(x2)" "s(x3)"
$ nsdf             : int 1
$ Ve               : num [1:37, 1:37] 9.76e-03 8.75e-13 2.34e-13 -1.99e-13 -1.09e-13 ...
$ Vp               : num [1:37, 1:37] 9.76e-03 6.55e-13 2.04e-13 -1.83e-13 -7.58e-14 ...
$ rV               : num [1:37, 1:37] -1.27e-25 -5.51e-19 5.64e-21 3.31e-19 -7.07e-20 ...
$ mgcv.conv        :List of 7
  ..$ full.rank      : int 37
  ..$ rank           : int 37
  ..$ fully.converged: logi TRUE
  ..$ hess.pos.def   : logi TRUE
  ..$ iter           : int 12
  ..$ score.calls    : int 12
  ..$ rms.grad       : num 1.74e-07
$ gcv.ubre         : num 4.05
$ aic              : num 1696
$ rank             : int 37
$ gcv.ubre.dev     : num 4.05
$ scale.estimated  : logi TRUE
$ method           : chr "GCV"
$ smooth           :List of 4
  ..$ :List of 24
  .. ..$ term          : chr "x0"
  .. ..$ bs.dim        : num 10
  .. ..$ fixed         : logi FALSE
  .. ..$ dim           : int 1
  .. ..$ p.order       : num 0
  .. ..$ by            : chr "NA"
  .. ..$ label         : chr "s(x0)"
  .. ..$ xt            : NULL
  .. ..$ id            : NULL
  .. ..$ sp            : Named num -1
  .. .. ..- attr(*, "names")= chr "s(x0)"
  .. ..$ drop.null     : num 0
  .. ..$ S             :List of 1
  .. .. ..$ : num [1:9, 1:9] 166.7 16.7 91.3 19.2 100 ...
  .. ..$ UZ            : num [1:402, 1:10] 0.922 0.897 0.88 0.879 0.872 ...
  .. ..$ Xu            : num [1:400, 1] -0.476 -0.473 -0.47 -0.47 -0.469 ...
  .. ..$ df            : num 9
  .. ..$ shift         : num [1(1d)] 0.483
  .. ..$ rank          : num 8
  .. ..$ null.space.dim: num 1
  .. ..$ plot.me       : logi TRUE
  .. ..$ side.constrain: logi TRUE
  .. ..$ S.scale       : num 0.896
  .. ..$ vn            : chr "x0"
  .. ..$ first.para    : num 2
  .. ..$ last.para     : num 10
  .. ..- attr(*, "class")= chr [1:2] "tprs.smooth" "mgcv.smooth"
  .. ..- attr(*, "qrc")=List of 4
  .. .. ..$ qr   : num [1:10, 1] -1.979 0.114 0.394 -0.129 -0.404 ...
  .. .. ..$ rank : int 1
  .. .. ..$ qraux: num 1.48
  .. .. ..$ pivot: int 1
  .. .. ..- attr(*, "class")= chr "qr"
  .. ..- attr(*, "nCons")= int 1
  ..$ :List of 24
  .. ..$ term          : chr "x1"
  .. ..$ bs.dim        : num 10
  .. ..$ fixed         : logi FALSE
  .. ..$ dim           : int 1
  .. ..$ p.order       : num 0
  .. ..$ by            : chr "NA"
  .. ..$ label         : chr "s(x1)"
  .. ..$ xt            : NULL
  .. ..$ id            : NULL
  .. ..$ sp            : Named num -1
  .. .. ..- attr(*, "names")= chr "s(x1)"
  .. ..$ drop.null     : num 0
  .. ..$ S             :List of 1
  .. .. ..$ : num [1:9, 1:9] 193.74 27.07 -96.04 7.76 -113.63 ...
  .. ..$ UZ            : num [1:402, 1:10] -1.109 -1.093 -1.086 -1.05 -0.992 ...
  .. ..$ Xu            : num [1:400, 1] -0.52 -0.518 -0.517 -0.513 -0.507 ...
  .. ..$ df            : num 9
  .. ..$ shift         : num [1(1d)] 0.521
  .. ..$ rank          : num 8
  .. ..$ null.space.dim: num 1
  .. ..$ plot.me       : logi TRUE
  .. ..$ side.constrain: logi TRUE
  .. ..$ S.scale       : num 0.884
  .. ..$ vn            : chr "x1"
  .. ..$ first.para    : num 11
  .. ..$ last.para     : num 19
  .. ..- attr(*, "class")= chr [1:2] "tprs.smooth" "mgcv.smooth"
  .. ..- attr(*, "qrc")=List of 4
  .. .. ..$ qr   : num [1:10, 1] 1.938 -0.104 -0.377 -0.118 0.389 ...
  .. .. ..$ rank : int 1
  .. .. ..$ qraux: num 1.48
  .. .. ..$ pivot: int 1
  .. .. ..- attr(*, "class")= chr "qr"
  .. ..- attr(*, "nCons")= int 1
  ..$ :List of 24
  .. ..$ term          : chr "x2"
  .. ..$ bs.dim        : num 10
  .. ..$ fixed         : logi FALSE
  .. ..$ dim           : int 1
  .. ..$ p.order       : num 0
  .. ..$ by            : chr "NA"
  .. ..$ label         : chr "s(x2)"
  .. ..$ xt            : NULL
  .. ..$ id            : NULL
  .. ..$ sp            : Named num -1
  .. .. ..- attr(*, "names")= chr "s(x2)"
  .. ..$ drop.null     : num 0
  .. ..$ S             :List of 1
  .. .. ..$ : num [1:9, 1:9] 225.9 31.8 113.3 14.5 132.4 ...
  .. ..$ UZ            : num [1:402, 1:10] 0.998 0.967 0.963 0.959 0.951 ...
  .. ..$ Xu            : num [1:400, 1] -0.498 -0.494 -0.494 -0.493 -0.492 ...
  .. ..$ df            : num 9
  .. ..$ shift         : num [1(1d)] 0.501
  .. ..$ rank          : num 8
  .. ..$ null.space.dim: num 1
  .. ..$ plot.me       : logi TRUE
  .. ..$ side.constrain: logi TRUE
  .. ..$ S.scale       : num 0.702
  .. ..$ vn            : chr "x2"
  .. ..$ first.para    : num 20
  .. ..$ last.para     : num 28
  .. ..- attr(*, "class")= chr [1:2] "tprs.smooth" "mgcv.smooth"
  .. ..- attr(*, "qrc")=List of 4
  .. .. ..$ qr   : num [1:10, 1] -2.025 0.1148 0.3837 -0.0849 -0.3948 ...
  .. .. ..$ rank : int 1
  .. .. ..$ qraux: num 1.46
  .. .. ..$ pivot: int 1
  .. .. ..- attr(*, "class")= chr "qr"
  .. ..- attr(*, "nCons")= int 1
  ..$ :List of 24
  .. ..$ term          : chr "x3"
  .. ..$ bs.dim        : num 10
  .. ..$ fixed         : logi FALSE
  .. ..$ dim           : int 1
  .. ..$ p.order       : num 0
  .. ..$ by            : chr "NA"
  .. ..$ label         : chr "s(x3)"
  .. ..$ xt            : NULL
  .. ..$ id            : NULL
  .. ..$ sp            : Named num -1
  .. .. ..- attr(*, "names")= chr "s(x3)"
  .. ..$ drop.null     : num 0
  .. ..$ S             :List of 1
  .. .. ..$ : num [1:9, 1:9] 202 37.3 98.5 -26.9 -134.5 ...
  .. ..$ UZ            : num [1:402, 1:10] -1.15 -1.13 -1.13 -1.1 -1.1 ...
  .. ..$ Xu            : num [1:400, 1] -0.503 -0.502 -0.501 -0.498 -0.498 ...
  .. ..$ df            : num 9
  .. ..$ shift         : num [1(1d)] 0.505
  .. ..$ rank          : num 8
  .. ..$ null.space.dim: num 1
  .. ..$ plot.me       : logi TRUE
  .. ..$ side.constrain: logi TRUE
  .. ..$ S.scale       : num 0.809
  .. ..$ vn            : chr "x3"
  .. ..$ first.para    : num 29
  .. ..$ last.para     : num 37
  .. ..- attr(*, "class")= chr [1:2] "tprs.smooth" "mgcv.smooth"
  .. ..- attr(*, "qrc")=List of 4
  .. .. ..$ qr   : num [1:10, 1] 2.002 -0.139 -0.372 0.151 -0.385 ...
  .. .. ..$ rank : int 1
  .. .. ..$ qraux: num 1.47
  .. .. ..$ pivot: int 1
  .. .. ..- attr(*, "class")= chr "qr"
  .. ..- attr(*, "nCons")= int 1
$ formula          :Class 'formula' length 3 y ~ s(x0) + s(x1) + s(x2) + s(x3)
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
$ var.summary      :List of 4
  ..$ x0: num [1:3] 0.00711 0.4615 0.99777
  ..$ x1: num [1:3] 0.000663 0.526806 0.99927
  ..$ x2: num [1:3] 0.00325 0.4958 0.99823
  ..$ x3: num [1:3] 0.00207 0.51989 0.99836
$ cmX              : Named num [1:37] 1 0 0 0 0 0 0 0 0 0 ...
  ..- attr(*, "names")= chr [1:37] "(Intercept)" "" "" "" ...
$ model            :'data.frame':        400 obs. of  5 variables:
  ..$ y : num [1:400] 7.13 2.97 3.98 10.43 14.57 ...
  ..$ x0: num [1:400] 0.185 0.702 0.573 0.168 0.944 ...
  ..$ x1: num [1:400] 0.6171 0.5691 0.154 0.0348 0.998 ...
  ..$ x2: num [1:400] 0.41524 0.53144 0.00325 0.2521 0.15523 ...
  ..$ x3: num [1:400] 0.132 0.365 0.455 0.537 0.185 ...
  ..- attr(*, "terms")=Classes 'terms', 'formula' length 3 y ~ 1 + x0 + x1 + x2 + x3
  .. .. ..- attr(*, "variables")= language list(y, x0, x1, x2, x3)
  .. .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
  .. .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. .. ..$ : chr [1:5] "y" "x0" "x1" "x2" ...
  .. .. .. .. ..$ : chr [1:4] "x0" "x1" "x2" "x3"
  .. .. ..- attr(*, "term.labels")= chr [1:4] "x0" "x1" "x2" "x3"
  .. .. ..- attr(*, "order")= int [1:4] 1 1 1 1
  .. .. ..- attr(*, "intercept")= int 1
  .. .. ..- attr(*, "response")= int 1
  .. .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
  .. .. ..- attr(*, "predvars")= language list(y, x0, x1, x2, x3)
  .. .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
  .. .. .. ..- attr(*, "names")= chr [1:5] "y" "x0" "x1" "x2" ...
$ control          :List of 15
  ..$ nthreads    : num 1
  ..$ irls.reg    : num 0
  ..$ epsilon     : num 1e-07
  ..$ maxit       : num 100
  ..$ trace       : logi FALSE
  ..$ mgcv.tol    : num 1e-07
  ..$ mgcv.half   : num 15
  ..$ rank.tol    : num 1.49e-08
  ..$ nlm         :List of 6
  .. ..$ ndigit           : num 7
  .. ..$ gradtol          : num 1e-06
  .. ..$ stepmax          : num 2
  .. ..$ steptol          : num 1e-04
  .. ..$ iterlim          : num 200
  .. ..$ check.analyticals: logi FALSE
  ..$ optim       :List of 1
  .. ..$ factr: num 1e+07
  ..$ newton      :List of 5
  .. ..$ conv.tol: num 1e-06
  .. ..$ maxNstep: num 5
  .. ..$ maxSstep: num 2
  .. ..$ maxHalf : num 30
  .. ..$ use.svd : logi FALSE
  ..$ outerPIsteps: num 0
  ..$ idLinksBases: logi TRUE
  ..$ scalePenalty: logi TRUE
  ..$ keepData    : logi FALSE
$ terms            :Classes 'terms', 'formula' length 3 y ~ 1 + x0 + x1 + x2 + x3
  .. ..- attr(*, "variables")= language list(y, x0, x1, x2, x3)
  .. ..- attr(*, "factors")= int [1:5, 1:4] 0 1 0 0 0 0 0 1 0 0 ...
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:5] "y" "x0" "x1" "x2" ...
  .. .. .. ..$ : chr [1:4] "x0" "x1" "x2" "x3"
  .. ..- attr(*, "term.labels")= chr [1:4] "x0" "x1" "x2" "x3"
  .. ..- attr(*, "order")= int [1:4] 1 1 1 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
  .. ..- attr(*, "predvars")= language list(y, x0, x1, x2, x3)
  .. ..- attr(*, "dataClasses")= Named chr [1:5] "numeric" "numeric" "numeric" "numeric" ...
  .. .. ..- attr(*, "names")= chr [1:5] "y" "x0" "x1" "x2" ...
$ pred.formula     :Class 'formula' length 2 ~x0 + x1 + x2 + x3
  .. ..- attr(*, ".Environment")=<environment: 0x000000001306ccf0>
$ pterms           :Classes 'terms', 'formula' length 3 y ~ 1
  .. ..- attr(*, "variables")= language list(y)
  .. ..- attr(*, "factors")= int(0)
  .. ..- attr(*, "term.labels")= chr(0)
  .. ..- attr(*, "order")= int(0)
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv>
  .. ..- attr(*, "predvars")= language list(y)
  .. ..- attr(*, "dataClasses")= Named chr "numeric"
  .. .. ..- attr(*, "names")= chr "y"
$ assign           : int 0
$ offset           : num [1:400] 0 0 0 0 0 0 0 0 0 0 ...
$ df.residual      : num 385
$ min.edf          : num 5
$ optimizer        : chr "magic"
$ call             : language gam(formula = y ~ s(x0) + s(x1) + s(x2) + s(x3), data = dat)
- attr(*, "class")= chr [1:3] "gam" "glm" "lm"
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群