Error: variable lengths differ

1,371 views
Skip to first unread message

Dong Liang

unread,
Apr 16, 2015, 8:59:17 PM4/16/15
to r-inla-disc...@googlegroups.com
Dear group,

I tried to build a SPDE model on a complex boundary.

The following piece of code (using the attached data) should generate an error.

Error in model.frame.default(new.fix.formula, data.same.len, na.action = inla.na.action) : 
  variable lengths differ (found for 'intercept')

load("question1.rda")
max.edge <- c(5000,1000000) ## max edge parameter
cutoff  <- 1000 ## cutoff parameter
mesh <- inla.mesh.2d(loc,boundary=boundary, max.edge=max.edge, cutoff=cutoff)
## define SPDE
spde <- inla.spde2.matern(mesh)
spatial.A <- inla.spde.make.A(mesh,loc)
## stack
n <- length(y)
stack <- inla.stack(data=list(y=y), A=list(spatial.A,1),
                    effects=list(list(i=1:spde$n.spde),list(E=E,intercept=rep(1,n),j=seq(1,n))),tag="data")
## define model
f.geo <- y ~ -1 + intercept + f(i,model=spde) + f(j,model="iid")
## fit model
dat <- inla.stack.data(stack)
ts0 <- proc.time()
r <- inla(f.geo,family="zeroinflatedpoisson0",
          data=dat,
          E=E,  verbose=T,
          control.predictor=list(compute=TRUE))
ts1 <- proc.time()


The same code worked on a boundary with simpler geometry.

Thanks
Dong
question1.rda

Finn Lindgren

unread,
Apr 17, 2015, 1:21:12 AM4/17/15
to Dong Liang, r-inla-disc...@googlegroups.com
You probably need
  E=dat$E
in the call to inla().

Finn

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at http://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.
<question1.rda>

Finn Lindgren

unread,
Apr 17, 2015, 2:51:27 AM4/17/15
to Dong Liang, r-inla-disc...@googlegroups.com
Also be aware that the scale of your spatial coordinates may lead to numerical problems. I would suggest scaling them down by a factor 1000.
Finn


On 17 Apr 2015, at 01:59, Dong Liang <dong.m...@gmail.com> wrote:

--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at http://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.
<question1.rda>

Dong Liang

unread,
Apr 17, 2015, 4:09:21 PM4/17/15
to r-inla-disc...@googlegroups.com
Finn,

I downscaled the shape files manually using the following code

load("question1u.rda")
max.edge <- c(5000,1000000)/1000 ## max edge parameter
cutoff  <- 1000/1000 ## cutoff parameter
loc <- loc/1000
b2 <- b1
b2@labpt <- b1@labpt/1000
b2@area <- b1@area/1000^2
b2@coords <- b1@coords/1000
boundary <- inla.sp2segment(b2)
mesh <- inla.mesh.2d(loc,boundary=boundary, max.edge=max.edge, cutoff=cutoff)
## define SPDE
spde <- inla.spde2.matern(mesh)
spatial.A <- inla.spde.make.A(mesh,loc)
## stack
n <- length(y)
stack <- inla.stack(data=list(y=y), A=list(spatial.A,1),
                    effects=list(list(i=1:spde$n.spde),list(E=E,intercept=rep(1,n),j=seq(1,n))),tag="data")
## define model
f.geo <- y ~ -1 + intercept + f(i,model=spde) + f(j,model="iid")
## fit model
dat <- inla.stack.data(stack)
ts0 <- proc.time()
r <- inla(f.geo,family="zeroinflatedpoisson0",
          data=dat,
          E=E,  verbose=T,
          control.predictor=list(compute=TRUE))
ts1 <- proc.time()


The same error message appeared.

Thanks
Dong
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion-group+unsub...@googlegroups.com.

To post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at http://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.
<question1.rda>
question1u.rda

Finn Lindgren

unread,
Apr 17, 2015, 4:15:31 PM4/17/15
to Dong Liang, r-inla-disc...@googlegroups.com
Two issues:
When building the stack, the  E=E  specification should be in the data list, not in the effects.
When using it in the inla() call, you need E=dat$E

The first issue is causing the problem.

Finn
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.

To post to this group, send email to r-inla-disc...@googlegroups.com.
Visit this group at http://groups.google.com/group/r-inla-discussion-group.
For more options, visit https://groups.google.com/d/optout.
<question1u.rda>

Dong Liang

unread,
Apr 17, 2015, 4:42:05 PM4/17/15
to r-inla-disc...@googlegroups.com, dong.m...@gmail.com
Finn,

Many thanks for help.

Adding A=inla.stack.A(stack) in the control.predictor fixed the error.

Please see following a working code.


max.edge <- c(5000,1000000)/1000 ## max edge parameter
cutoff  <- 1000/1000 ## cutoff parameter
loc <- loc/1000
b2 <- b1
b2@labpt <- b1@labpt/1000
b2@area <- b1@area/1000^2
b2@coords <- b1@coords/1000
boundary <- inla.sp2segment(b2)
mesh <- inla.mesh.2d(loc,boundary=boundary, max.edge=max.edge, cutoff=cutoff)
## define SPDE
spde <- inla.spde2.matern(mesh)
spatial.A <- inla.spde.make.A(mesh,loc)
## stack
n <- length(y)
stack <- inla.stack(data=list(y=y,E=E), A=list(spatial.A,1),
                    effects=list(list(i=1:spde$n.spde),list(intercept=rep(1,n),j=seq(1,n))),tag="data")

## define model
f.geo <- y ~ -1 + intercept + f(i,model=spde) + f(j,model="iid")
## fit model
dat <- inla.stack.data(stack)
ts0 <- proc.time()
r <- inla(f.geo,family="zeroinflatedpoisson0",
          data=dat,
          E=dat$E,  verbose=T,
          control.predictor=list(A=inla.stack.A(stack),compute=TRUE))
ts1 <- proc.time()
<question1u.rda>
Reply all
Reply to author
Forward
0 new messages