Skip to content

Error in names(x) <- value: 'names' attribute must be the same length as the vector in gam function

2 messages · 王 嘉炜, William Dunlap

#
Dear R users,


I am using the *mgcv package* to model the ozone pollution concentration according to some environmental covariates. The model takes the form :

model1 <-gam(O3~s(X,Y,bs="tp",k=10)+wd+s(date,bs="cc",k=100)+district,data=mydata,family= gaussian(link ="log" ),na.action="na.omit", method="REML")

And here is the strcture of  covariates
'data.frame': 7100 obs. of  286 variables:
 $ date            : Date, format: "2016-01-01" "2016-01-01" "2016-01-01" ...
 $ O3              : num  0.0141 0.0149 0.0102 0.0159 0.0186 ...
 $ district        : Factor w/ 10 levels "bc","bh","dl",..: 1 8 7 8 2 6 4 4 10 2 ...
 $ wd              : Factor w/ 16 levels "E","ENE","ESE",..: 13 13 13 13 13 2 9 9 11 13 ...
 $ X               : num  0.389 0.365 1 0.44 0.892 ...
 $ Y               : num  0.311 0.204 0.426 0.223 0.162 ...

I am stuck on an error in R: 'names' attribute [1] must be the same length as the vector [0].

I try to find where the problem is by delete the term of "s(date,bs="cc",k=100)" from the fomular and it could work well. It seems like  there is something wrong with date field.

I'm not exactly sure how to fix this problem.  Any advice would be greatly
appreciated!
Jiawei Wang
???? Outlook<http://aka.ms/weboutlook>
#
mgcv::s() does not appear to work with objects of class "Date".  E.g.,
response=log2(1:10)%%1)
Error in names(dat) <- object$term :
  'names' attribute [1] must be the same length as the vector [0]
6: ExtractData(object, data, knots)
5: smooth.construct3(object, data, knots)
4: smoothCon(split$smooth.spec[[i]], data, knots, absorb.cons,
scale.penalty = scale.penalty,
       null.space.penalty = select, sparse.cons = sparse.cons,
diagonal.penalty = diagonal.penalty,
       apply.by = apply.by, modCon = modCon)
3: gam.setup(formula = list(pf = response ~ 1, pfok = 1, smooth.spec = list(
       list(term = "date", bs.dim = -1, fixed = FALSE, dim = 1L,
           p.order = NA, by = "NA", label = "s(date)", xt = NULL,
           id = NULL, sp = NULL)), fake.formula = response ~ 1 +
       date, response = "response", fake.names = "date", pred.names =
"date",
       pred.formula = ~date), pterms = response ~ 1, data = list(
       response = c(0, 0, 0.584962500721156, 0, 0.321928094887362,
       0.584962500721156, 0.807354922057604, 0, 0.169925001442312,
       0.321928094887362), date = c(17885L, 17892L, 17899L, 17906L,
       17913L, 17920L, 17927L, 17934L, 17941L, 17948L)), knots = NULL,
       sp = NULL, min.sp = NULL, H = NULL, absorb.cons = TRUE, sparse.cons
= 0,
       select = FALSE, idLinksBases = TRUE, scale.penalty = TRUE,
       paraPen = NULL, drop.intercept = FALSE)
2: do.call(gsname, list(formula = gp, pterms = pterms, data = mf,
       knots = knots, sp = sp, min.sp = min.sp, H = H, absorb.cons = TRUE,
       sparse.cons = 0, select = select, idLinksBases =
control$idLinksBases,
       scale.penalty = control$scalePenalty, paraPen = paraPen,
       drop.intercept = drop.intercept))
1: gam(data = d, response ~ s(date))

You might work around this by using as.numeric(date) instead of date.

Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Fri, Jan 4, 2019 at 12:29 AM ? ?? <wangjiawei92 at hotmail.com> wrote: