[R-sig-Geo] No neighbours in sample using spatcounts package in R

chris english englishchristophera at gmail.com
Thu Oct 20 12:20:17 CEST 2016


Nick,

I get the same error running your code.

set.seed(987654321)

library(spatcounts)
data("sim.Yin")
data("sim.fm.X")
data("sim.gmat")
data("sim.nmat")
data("sim.region")
AllData <- cbind(sim.Yin, sim.region, sim.fm.X)
colnames(AllData)[1:2] <- c("Yin", "Region")

idx <- sample(1:nrow(AllData), 100, replace=TRUE)
newdata.df <- AllData[idx,]
newdata.df <- newdata.df[order(newdata.df$Region),]
X <- as.data.frame(newdata.df[,3:4])
region <- as.data.frame(newdata.df$Region)
colnames(region) <- "V1"
Yin <- as.data.frame(newdata.df$Yin)

temp.idx <- sort(unique(newdata.df$Region))
g.temp <- sim.gmat[temp.idx,temp.idx]

TotalN <- rowSums(g.temp) ##CHECK IF THERE ARE ZEROS PRESENT
> is.element(0, n.temp$V6)
[1] TRUE # after a few run thru's of idx to is.element

n.temp <- sim.nmat[temp.idx,]
n.temp$V2 <- temp.idx[match(n.temp$V2, temp.idx)]
n.temp$V3 <- temp.idx[match(n.temp$V3, temp.idx)]
n.temp$V4 <- temp.idx[match(n.temp$V4, temp.idx)]
n.temp$V5 <- temp.idx[match(n.temp$V5, temp.idx)]
n.temp$V6 <- TotalN

> class(n.temp$V1)
[1] "numeric"
> class(n.temp$V2)
[1] "integer"
> class(n.temp$V3)
[1] "integer"
> class(n.temp$V4)
[1] "integer"
> class(n.temp$V5)
[1] "integer"
> class(n.temp$V6)
[1] "numeric"

# here, essentially checking what NA might be replaced with by class
# and wondering if a small numeric is desirable (0.001), $V1 & $v6
# and your 0 for integers ($V2-V5)

Well, a bunch of different tries and the error persists.

> n.temp[is.na(n.temp)] <- 1
> n.temp$V6[7] <- 10
> n.temp$V6[24] <- 10
> n.temp$V6[28] <- 0.001
>
> n.temp$V6[28] <- 10

> which(n.temp$V6 == 0)
integer(0)


> Yin.NB <- est.sc(Yin, ~ X[,1] + X[,2] -1,
+ region, model="NB", g.temp, n.temp, totalit=10) ##ERROR
Error: NA/NaN/Inf in foreign function call (arg 1)

So, i guess i'd do a debugonce(est.sc) and find out which foreign function
is disappointing or
disappointed by the inputs, but the culprit does not appear to be TotalN.

HTH,

Chris



On Wed, Oct 19, 2016 at 4:01 PM, Nick van Doormaal <
nick.vandoormaal at gmail.com> wrote:

> Dear list members,
>
> I'm trying to do a bootstrap of a spatial count model using the
> *spatcounts-package* in R. However, resampling with replacement may lead to
> "islands", because sometimes no neighbors will be selected. I believe this
> is causing the error message: Error: NA/NaN/Inf in foreign function call
> (arg 1). Can somebody confirm this if this indeed the case? If so, is there
> a way to get around it, so that I would still be able to carry out a
> bootstrap?
>
> Please find below the code to recreate the problem using the example
> dataset of the spatcounts package.
>
> Thank you for your time and I hope somebody can help me out a bit.
>
> ####START CODE#####
> set.seed(987654321)
>
> library(spatcounts)
>
> AllData <- cbind(sim.Yin, sim.region, sim.fm.X)
> colnames(AllData)[1:2] <- c("Yin", "Region")
>
> idx <- sample(1:nrow(AllData), 100, replace=TRUE)
> newdata.df <- AllData[idx,]
> newdata.df <- newdata.df[order(newdata.df$Region),]
> X <- as.data.frame(newdata.df[,3:4])
> region <- as.data.frame(newdata.df$Region)
> colnames(region) <- "V1"
> Yin <- as.data.frame(newdata.df$Yin)
>
> temp.idx <- sort(unique(newdata.df$Region))
> g.temp <- sim.gmat[temp.idx,temp.idx]
>
> TotalN <- rowSums(g.temp) ##CHECK IF THERE ARE ZEROS PRESENT IN THIS
> VECTOR.##OTHERWISE RUN AGAIN UNTIL AT LEAST ONE ZERO
>
> n.temp <- sim.nmat[temp.idx,]
> n.temp$V2 <- temp.idx[match(n.temp$V2, temp.idx)]
> n.temp$V3 <- temp.idx[match(n.temp$V3, temp.idx)]
> n.temp$V4 <- temp.idx[match(n.temp$V4, temp.idx)]
> n.temp$V5 <- temp.idx[match(n.temp$V5, temp.idx)]
> n.temp$V6 <- TotalN
>
> n.temp[is.na(n.temp)] <- 0
>
> Yin.NB <- est.sc(Yin, ~ X[,1] + X[,2] -1,
> region, model="NB", g.temp, n.temp, totalit=10) ##ERROR
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list