1

I'm trying to run a lasso model for spatial (polygons) data in R. It has been very very difficult to find a package that allows me to do run the spatial lasso model and at the same time allows me to get the Beta coefficients for my independent variables. Which is my final goal: using lasso to evaluate which variables are important for the model and which are not, considering the spatial relationship in my data.

I have tried with the following:

  1. glmnetcv function from the spm2 package.
  2. scgwr_p function from the scgwr package.
  3. lasso.ss function from the spselect package.

From the example the spm2 package provides:

library(spm2)

data(petrel)
x <- as.matrix(petrel[, c(1, 2, 6:9)]) #Columns 1 and 2 are longitude and latitude##
y <- log(petrel[, 5] + 1) #Column 1 is the dependent variable
set.seed(1234)
glmnetcv1 <- glmnetcv(x, y, validation = "CV",  predacc = "ALL", alpha = 1)

Given this code, I would like to get information of which of the variables (from columns 6 to 9) are important in this model. And which variables are not.

If there is another package that I'm missing that can deal with spatial data (polygons) and lasso models, please let me know!

Any help would be so much appreciated!!!

1 Answers1

0

I was able to make it using either the CAST or caret packages:

library(sf)
library(CAST)
library(caret)

#Loading data
nc <- st_read(system.file("shape/nc.shp", package="sf"))

#Training and test data
set.seed(100)
ind   <- sample(2,nrow(nc),replace=T,prob = c(0.7,0.3))
train <- nc[ind==1,]
test  <- nc[ind==2,]

predictors <- c("SID74","BIR79","BIR74")
response   <- "NWBIR79"

#1st option #
set.seed(10)
CAST::ffs(st_drop_geometry(train[,predictors]), train$NWBIR79,method = "lasso")

#2nd option:
model <- train(st_drop_geometry(train[,predictors]), train$NWBIR79, method="lasso", trControl=trainControl(method = "cv"))