[R-sig-Geo] No neighbours in sample using spatcounts package in R
Nick van Doormaal
nick.vandoormaal at gmail.com
Fri Oct 21 10:46:39 CEST 2016
Hi Chris,
Thank you very much for your time and effort to help me out. Much
appreciated!
When I checked the structure of the example dataset, it seems that all the
variables are numerical. That's why I thought that replacing all the NAs
with 0s would be an okay way.
library(spatcounts)
data(sim.nmat)
str(sim.nmat)
'data.frame': 100 obs. of 6 variables:
$ V1: num 1 2 3 4 5 6 7 8 9 10 ...
$ V2: num 11 12 13 14 15 16 17 18 19 20 ...
$ V3: num 2 1 2 3 4 5 6 7 8 9 ...
$ V4: num 0 3 4 5 6 7 8 9 10 0 ...
$ V5: num 0 0 0 0 0 0 0 0 0 0 ...
$ V6: num 2 3 3 3 3 3 3 3 3 2 ...
str(n.temp)
'data.frame': 63 obs. of 6 variables:
$ V1: num 1 2 7 13 14 16 19 20 21 22 ...
$ V2: num 1 1 1 1 1 1 1 1 1 1 ...
$ V3: num 2 1 1 23 1 26 1 30 31 32 ...
$ V4: num 1 1 1 1 13 1 1 19 22 21 ...
$ V5: num 1 1 1 14 1 1 20 1 1 23 ...
$ V6: num 1 1 0 2 1 1 1 2 2 3 ...
I think it has something to do with the combination of nmat (ntemp) and
gmat (gtemp)...but I can't really figure out how or why.
On 20 October 2016 at 12:20, chris english <englishchristophera at gmail.com>
wrote:
> 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