inlabru-org / inlabru

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

Grouping or indexing spatial random effects for prediction with "predict" function. #70

Closed shinneuro closed 3 years ago

shinneuro commented 4 years ago

Dear inlabru,

Thanks to your help, I am getting results! But one concern is that I have difficulty grouping spatial random effects and predicting with predict function.

For example, I have a set of coordinates and they are either from effect A or B. 1) I have tried using group argument in component: In this case, components <- coordinates ~ Intercept + s(map = coordinates, group = "effect", model = spde) worked but I have no idea how to generate posteriors using predict with respect to each effect A or B. I checked that two hyperparameters theta1 and theta2 were sampled but I do not know how to access them. 2) Alternatively, I tried to use index to pointing each effect after I found the function in effect.R file. For indexing I generated two columns for two effects, which contains 1 or NA. So, I tried components <- coordinates ~ Intercept + s1(map = coordinates, index = "A", model = spde) + s2(map = coordinates, index = "B", model = spde) but it didn't work.

Thank you again for building this wonderful tool! Yours Sincerely, Minho

finnlindgren commented 4 years ago

Hi, I believe your component specification should be components <- coordinates ~ Intercept + s(map = coordinates, group = effect, model = spde) and for the prediction, supply a Spatial*DataFrame with a column effect equal to 1 for the coordinates belonging to "A" and equal to 2 for "B", just as for the data frame you use when estimating the model. I other words, the data supplied to predict() needs to take a similar format as your actual data. For example, to extract a spatial grid of predictions for effect 1 only:

df_raw <- pixels(mesh)
df_predict <- SpatialPixelsDataFrame(df_raw, data.frame(effect = rep(1, length(df_raw))))
... <- predict(fit, df_predict, ...)
shinneuro commented 4 years ago

Hi, finn. Once again, thank you for your quick response! The problem is that I am estimating two separate effect for THE SAME MAP. And I forgot to mention that therefore I added control.group = list(model = "iid") in the random spatial effects, ending components <- coordinates ~ Intercept + s(map = coordinates, group = "effect", model = spde, control.group = list(model = "iid")) For now, I ended up with generating mesh that contains all the coordinates that I want, and I just am using estimates from the lgcp object.

shinneuro commented 4 years ago

Also, for your reference, for the group effect, group = effect does not work with the error like that R cannot find effect object. group = "effect" worked without error, which is a column within the spatialdataframe provided in lgcp function.

finnlindgren commented 3 years ago

From version 2.3.1, the predict method has an _eval feature for each component that allows evaluating the result at arbitrary values (e.g. new coordinates and group-values), like s_eval(main=your_coordinates, group = rep("A", nrow(your_coordinates)). The interface has been updated quite a lot in versions >= 2.3.0.

shinneuro commented 3 years ago

Dear Finn,

Thanks!

I was directly working with INLA, but now inlabru seems more powerful!!

I will give it a try!