inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
76 stars 21 forks source link

lgcp() throwing abigious error when model is not pre-defined in components #40

Closed HelloMckenna closed 1 year ago

HelloMckenna commented 6 years ago

Hello inlabru,

I'm trying to run a demo example with my own data and kept getting error:

Error in paste("\n\tModel [", model, "] is not of type character()\n", : cannot coerce type 'closure' to vector of type 'character'

Would it be possible for inlabru to print a more useful error message? I figured out that I was getting this error due to not defining the matern model before making it part of the components:

cmp <- coordinates ~ mySmooth(map = coordinates, model = matern) + Intercept

which then throws the error when running

fit <- lgcp(cmp, Points, samplers = boundary)

This example below will throw the error.

# START
library(INLA)
library(inlabru)
library(sp)

cmp <- coordinates ~ mySmooth(map = coordinates, model = matern) + Intercept

# Generate Random Points Spatial Points Dataframe with dummy data
Points <- cbind(x <- runif(25,-10,10), y <- runif(25,-10,10)) # Random Points
Points <- SpatialPointsDataFrame(Points, data = as.data.frame(runif(25,0,1)), proj4string = CRS(as.character("+proj=utm +zone=32N +datum=WGS84 +units=km +ellps=WGS84 +towgs84=0,0,0")))

# Generate Spatial Polygons Dataframe for boundary
xx <- c(10, 10, -10, -10, 10); yy <- c(-10, 10, 10 ,-10, -10)
p <- Polygon(cbind(xx,yy))
ps <-  Polygons(list(p),1);
sps <-  SpatialPolygons(list(ps), proj4string = CRS(as.character("+proj=utm +zone=32N +datum=WGS84 +units=km +ellps=WGS84 +towgs84=0,0,0")))
PolygonData <- as.data.frame(1);colnames(PolygonData) <- "weight"; row.names(PolygonData) <- "tmp" 
boundary <- SpatialPolygonsDataFrame(sps, PolygonData, match.ID = F)

fit <- lgcp(cmp, Points, samplers = boundary)

# Error in paste("\n\tModel [", model, "] is not of type character()\n",  : 
#                  cannot coerce type 'closure' to vector of type 'character'

#END 
finnlindgren commented 6 years ago

Hi, this is tricky, since the error comes from the R language internals in a way that's difficult to detect. In fact, the error here is thrown by code that was intended to give a more informative error message, but it failed to construct the error message string, due to the error it was trying to report!

Can you supply the "traceback()" output to more easily pinpoint the call structure and error location?

HelloMckenna commented 6 years ago

Certainly

traceback() 10: paste("\n\tModel [", model, "] is not of type character()\n", sep = "") 9: stop(paste("\n\tModel [", model, "] is not of type character()\n", sep = "")) 8: inla.is.model(model, "latent", stop.on.error = TRUE) 7: INLA::f(xxx, ..., group = group, model = model) 6: g(mySmooth, map = coordinates, model = matern) at #1 5: eval(expr, envir, enclos) 4: eval(parse(text = lb), envir = env) 3: make.model(components) 2: like("cp", formula = formula, data = data, samplers = samplers, components = components, E = E, ips = ips, domain = domain) 1: lgcp(cmp, Points, samplers = boundary)

finnlindgren commented 6 years ago

The error message is generated inside the INLA package itself; what’s your OS, R version, and package.version(“inlabru”) and package.version(“INLA”)?