I am trying to model the distribution of points data, according to years and a couple of covariates. I started to use inlabru as it generally looked way easier to run lgcp. My model for one year and all covariate, or basic SPDE model for multiples years are working well, but I don’t know how to include covariate which are changing among years.
First, I’ve selected data for one year and include the different covariables using personalized function, as in https://inlabru-org.github.io/inlabru/articles/web/2d_lgcp_covars.html. Per example, this function is looking at the percentage of deforestation around each observation points (I’m creating 1km buffer around the points because it corresponds to the precisions of my satellite precision).
f.defo<-function(x,y){
# Turn the coordinates into sptial
point object with appropriate crs
spp <-
SpatialPoints(data.frame(x = x, y = y), proj4string = fm_sp_get_crs(defo2))
proj4string(spp)
<- fm_sp_get_crs(defo2)
sf_spp<-st_as_sf(spp)
# Extract the
proportion of pa around one point (I do it using sf pakage)
a<-st_buffer(sf_spp,1000)
b<-st_intersection(a,defo)
if(nrow(b)==0){
return(0) }
else{b<-st_union(b)
return<-as.numeric(st_area(b))/as.numeric(st_area(a))}}
Then I’m fitting the model and the results look fine!
cmp<-coordinates~mySPDE(main=coordinates,model=matern)+
settlement(f.settlement(x,y),model="linear")
The issue come when I run the model for several years. I haven’t succeed to identified a way to rewrite the f.defo function so for every year, it will subset my data and only return the value of deforestation events happening the same year than the point was recorde. I think the closer solution that I though was this
f.defo<-function(x,y,year){
# Subset defo dataset based on year argument in the lgcp formula
defo_temp<-subset(defo,year==year)
# Turn the coordinates into sptial
point object with appropriate crs
spp <-
SpatialPoints(data.frame(x = x, y = y), proj4string = fm_sp_get_crs(defo2))
proj4string(spp)
<- fm_sp_get_crs(defo2)
sf_spp<-st_as_sf(spp)
# Extract the
proportion of pa around one point (I do it using sf pakage)
a<-st_buffer(sf_spp,1000)
b<-st_intersection(a,defo)
if(nrow(b)==0){
return(0) }
else{b<-st_union(b)
return<-as.numeric(st_area(b))/as.numeric(st_area(a))}}
Then I am creating the formula of the model, including a year argument compare to before.
cmp <- coordinates+year ~ defo(f.defo(x,y,year),model="linear")+ mySPDE(main=coordinates, model = matern, group = year, ngroup = nyear, control.group=list(model="ar1"))
Finally I'm fitting the model (with ips havong a year argument)
fit <- lgcp(cmp, df,ips,domain=list(coordinates=mesh,year=df$year))
However, when I return the summary.fixed of the model, value
of intercept and defo are the same (and similar issue with other types of
variables), indicating that my model is not working as expected. I think it’s an
issue with the way I’m calling year into the function but I couldn’t find an
example online on how to do this.
If you could give me a hint on how to use temporal index in the covariate definition function to have a spatio-temporal model with covariate, that would be great! I seem to don't wrap my head aroudn the way group argument are used in the inlabru argument.
Thanks in advance!
--
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 view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/42e4ff10-2611-407a-9f0d-30ecb6a45332n%40googlegroups.com.
Hello Finn and thank you for the response,
Now I see my mistake, I wasn’t sure how the function was working internally, but now I understand that lgcp ‘feed” the function with a vector with all x, y and years, make sense why my code wasn’t working…
I succeeded to rewrite my function so it returns a vector of the value, and it seems to be working (I’ve got different summary for the fixed effect, now I’ll include other covariance and see further results). Sorry for the syntax, this function is quite horribly written, and I have relied on the use of the sf package as I’m most familiar with it.
f.settlement<-function(x,y,year){
spp <- SpatialPoints(data.frame(x
= x, y = y), proj4string = fm_sp_get_crs(settlement2))
spp$year<-year2
proj4string(spp) <- fm_sp_get_crs(settlement2)
sf_spp<-st_as_sf(spp)
# create the buffer and index them
a<-st_buffer(sf_spp,1000)
total_area<-as.numeric(st_area(a[1,]))
a$index<-1:nrow(a)
# Intersection of
different objects with the year
b<-st_intersection(a,settlement_total[,'year'==a$year])
b$area<-as.numeric(st_area(b))
b<-st_drop_geometry(b)
a<-st_drop_geometry(a)
b<-b%>%
group_by(index,year)%>%
summarise(total_area=sum(area))
# Create a loop to
associate settlement of each buffer zone to variable
ibis=1
for (i in 1:nrow(a)){
if(any(b[,'index']==a[i,"index"])){
a[i,"settlement"]<-b[ibis,"total_area"]/total_area
ibis=ibis+1
}else{
a[i,"settlement"]<-0}}
return(as.vector(a$settlement))}
By any chance, do you have an example of a generic function to catch the covariance value in a spatio-temporal model such as the one we can find in the covariance tutorial (https://inlabru-org.github.io/inlabru/articles/web/2d_lgcp_covars.html)? I think it might be helpful for people starting with inlabru.
Concerning the output I haven’t got any error message, even if my function was not working. However, I could see that all fixed effects have almost similar impact, while when specified properly we have different coefficient. I’m using the version 2.3.1 of inlabru.
Also I wanted to thank you for the amazing work on the inlabru package, it does really make points process model way easier.
Michel
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/cf3c88f9-5047-471e-a948-213677535066n%40googlegroups.com.
Dear Finn,
I know my case is quite specific and I’ve spent some time to process my data before starting the modelling so it’s probably hard to generalize from other cases. Anyhow, thank you for the patience and helpful answer.
I’ve tried to use the development versions but doesn’t display warning message if I have an issue with my inputs (but it can still be seen by the similarity of the intercept with coefficient of problematic variables).
The purpose of b<-st_intersection(a,settlement_total[,'year'==a$year]) is to get the overlap between the object a (in my cases buffers zones of 1 km around the point indicated by the coordinates) and the settlement for the year given in the argument (selecting the different rows for this year, each row corresponding to a polygon). This is using a simple feature object, implemented by the sf package. This package contains more or less the same feature as the sp package, but the syntax and data structure is slightly different. However it’s easy to convert the object from one type to another.
Thanks to your explanation, I’ve been able to
create function for a couple of covariate (by extracting value from raster,
calculating coverage in a 1 km radius and determining distance between points
and lines features).
Best regards,
Michel
To view this discussion on the web, visit https://groups.google.com/d/msgid/r-inla-discussion-group/CAHC4gzatTNExpMfFYP31syFyuSWyDEm5u%2Bn8yPquuetsRg7d5A%40mail.gmail.com.