Calculation of Moran's I with 4000 records
Asked Answered
C

1

11

I have 4000 records of volume in trees plantation. I need to calculate the Moran's I to the whole plantation. I use ape library because spdep is said to be slower. My code is this:

# Modified from http://www.ats.ucla.edu/stat/r/faq/morans_i.htm
require(ape)
df <- data.frame(
     x = 1:2000,
     y = 1:2000,
     v = rnorm(4000, mean=4) )
df.dists <- as.matrix(dist(cbind(df$x, df$y)))
df.dists.inv <- 1/df.dists
diag(df.dists.inv) <- 0
Moran.I(df$v, df.dists.inv)

When I run the code, I get overflow-like errors.

*Error in if (obs <= ei) 2 * pv else 2 * (1 - pv) : 
  missing value where TRUE/FALSE needed*

Using ff library

require(ape)
require(ff)
ffdf <- as.ffdf(df)
ffdf.dists <- as.matrix(dist(cbind(ffdf$x, ffdf$y)))
ffdf.dists.inv <- 1/df.dists
diag(ffdf.dists.inv) <- 0
Moran.I(ffdf$v, ffdf.dists.inv)

More error messages:

*Error in x - m : non-numeric argument to binary operator
In addition: Warning message:
In mean.default(x) : argument is not numeric or logical: returning NA*
  • How can I get the calculation to the whole plantation?

  • Should I use sdep instead of ape library?

  • How could parallel library solve this problem?

Thanks in advance Juan

Choir answered 21/9, 2013 at 11:35 Comment(1)
You should set.seed to make your problem reproducible. No need to use big matrix ( you can use a small matrix and you will get the same problem once you have some Infinite values).Permian
P
11

You have some infinite values in your matrix. This should work in the 2 cases ( with and without ff package)

df.dists.inv[is.infinite(df.dists.inv)] <- 0

Applying this with a small example:

require(ape)
set.seed(1)
df <- data.frame(
  x = 1:10,
  y = 1:10,
  v = rnorm(20, mean=4) )
.....

df.dists.inv[is.infinite(df.dists.inv)] <- 0
Moran.I(df$v, df.dists.inv)

$observed
[1] -0.02246154

$expected
[1] -0.05263158

$sd
[1] 0.05399303

$p.value
[1] 0.5763143
Permian answered 21/9, 2013 at 11:46 Comment(5)
It is so nice! A simple error in weight calculation. Tnank you!Choir
You are welcome! I see the error because I tried t reproduce the problem in a small matrix.Permian
Thank you @egstudy. Even that your code solved and I do not need ff library, when I run the code with ff and the infinite elimination, I get this error message: Error in x - m : non-numeric argument to binary operator In addition: Warning message: In mean.default(x) : argument is not numeric or logical: returning NAChoir
you get an error because cbind(ffdf$x, ffdf$y) give an empty object...` and hence ffdf.dists <- as.matrix(dist(cbind(ffdf$x, ffdf$y)))` is an empty matrix..Permian
Then, I see no adventage on using ff in this code. The problem could be to process more than 10000 records. Ff should be able to arise that problem.Choir

© 2022 - 2024 — McMap. All rights reserved.