'error: 'predictions' contains NA while predict landuse change using glm model (lulcc package) in R

I made a prediction model for land use change using the lulcc package in R. Prediction was done using glm. When I did the glm.pred (the last line), there was an error: 'predictions' contains NA message. I've tried using the na.omit function, but there are still errors and I don't know where the NA value is located. Are there any suggestions or solutions to my problem?

Here is the code

setwd("D:\\Penelitian_Tesis\\lulccLMG package")

memory.size() ### Checking your memory size
memory.limit() ## Checking the set limit
memory.limit(size=500000) ### expanding your memory

lu_lmg_2007 = raster("lu_lmg2007.tif")
lu_lmg_2013 = raster("lu_lmg_2013.tif")
lu_lmg_2019 = raster("lu_lmg_2019.tif")
ef_001 = raster("ef_001.tif")
ef_002 = raster("ef_002.tif")
ef_003 = raster("ef_003.tif")

# create raster stack
Lmg <- stack(lu_lmg_2007,lu_lmg_2013,lu_lmg_2019)
save(Lmg,file="Lmg.Rda")
na.omit("Lmg")

Expvar <- stack(ef_001,ef_002,ef_003)
save(Expvar,file="Expvar.Rda")
na.omit("Expvar")

##############

library(rgdal)

library(raster)

load(file="Lmg.Rda")
load(file="Expvar.Rda")

library(lulcc)

## observed maps
obs <- ObsLulcRasterStack(x=Lmg,
                          pattern="lu",
                          categories=c(0,1,2,3,4,5,6,7),
                          labels=c("HU","LAD","LN","LA","TE","SW","SB","TA"),
                          t=c(0,6,12))
obs
na.omit(obs)
plot(obs)

crossTabulate(obs, times=c(0,12))

## explanatory variables
ef <- ExpVarRasterList(x=Expvar, pattern="ef")
ef
na.omit(ef)

part <- partition(x=obs[[1]], size=0.1, spatial=TRUE)
na.omit("part")
train.data <- getPredictiveModelInputData(obs=obs, ef=ef, cells=part[["train"]],t=0)
na.omit(train.data)

forms <- list(HU ~ ef_001+ef_002+ef_003,
              LAD ~ ef_002+ef_003,
              LN ~ ef_002+ef_003,
              LA ~ ef_002+ef_003,
              TE ~ ef_002+ef_003,
              SW ~ ef_002+ef_003,
              SB ~ ef_001+ef_002+ef_003,
              TA ~ ef_001+ef_002+ef_003)

glm.models <- glmModels(formula=forms, family=binomial(link="logit"), data=train.data, obs=obs)
na.omit(glm.models)

## test ability of models to predict allocation of forest, built and other
## land uses in testing partition
test.data <- getPredictiveModelInputData(obs=obs, ef=ef, cells=part[["test"]])
na.omit(test.data)

glm.pred <- PredictionList(models=glm.models, newdata=test.data)


Solution 1:[1]

I encountered the same problem. The problem is your train.data OR test.data might CONTAIN NA.

The solution is change na.omit(train.data) to train.data.no.na <- na.omit(train.data) and use newly created variable to feed your model.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Majid Hajibaba