How to create multiple owin for clark and evans test?

124 views Asked by At

I am trying to avoid running ~250 Clark and Evans tests (clarkevans.test) manually.

I have a table of xmin, xmax, ymin, ymax coordinates in an excel file where each row are the dimensions an operating window.

After reading the excel file (read.csv) into R I can't seem to get any form of "apply" and "owin" to work in conjunction to output an owin for each row. Eventually I'll need to create ppp and run clarkevans.test in a similar fashion, but right now I just need help on this first step.

coordin<-read.csv("Coordin.csv")
cdf<-data.frame(coordin)
> cdf
      xmin   xmax    ymin    ymax
1   456741 456841 3913505 3913605
2   453341 453441 3915805 3915905
3   453441 453541 3915805 3915905
4   452441 452541 3915705 3915805
5   453741 453841 3915705 3915805

I've tried several variations, but I can't get anything to work.

lapply(cdf, function(x) owin(xmin, xmax, ymin, ymax)) 
2

There are 2 answers

0
Ege Rubak On

I would recommend a for-loop for this since you easily can add steps to generate ppp objects when you get that far:

library(spatstat)
# Test data:
dat <- data.frame(xmin = 1:3, xmax = 2:4, ymin = 1:3, ymax = 2:4)
# List of owin initialised as unit squares:
win_list <- replicate(nrow(dat), owin(), simplify = FALSE)
# For loop to make each owin:
for(i in seq_len(nrow(dat))){
  # Vector of owin values:
  v <- as.numeric(dat[i, ])
  # Finally create the owin object
  win_list[[i]] <- owin( v[1:2], v[3:4])
}

Then the list of owin objects contains exactly what you expect:

win_list
#> [[1]]
#> window: rectangle = [1, 2] x [1, 2] units
#> 
#> [[2]]
#> window: rectangle = [2, 3] x [2, 3] units
#> 
#> [[3]]
#> window: rectangle = [3, 4] x [3, 4] units

If you insist on using apply:

apply(dat, 1, function(x) owin(c(x[1], x[2]), c(x[3], x[4])))
#> [[1]]
#> window: rectangle = [1, 2] x [1, 2] units
#> 
#> [[2]]
#> window: rectangle = [2, 3] x [2, 3] units
#> 
#> [[3]]
#> window: rectangle = [3, 4] x [3, 4] units
0
Adrian Baddeley On

The original code didn't work because owin(xmin,xmax,ymin,ymax) is not valid syntax for calling owin.

One valid syntax is owin(c(xmin,xmax), c(ymin,ymax)).

The following would work on a data frame df whose columns are xmin,xmax,ymin,ymax:

apply(df, 1, function(z) owin(z[1:2], z[3:4])