Pepe
Pepe

Reputation: 15

How to subset point patterns of a hyperframe using marks from a dataframe

I have a hyperframe with 93 rows. Each row contains a stem map of trees of class ppp along with plot level grouping factors. A dataframe of marks provides point specific data, such as the diameter, species, and height for each point. I need to subset the point pattern based on the dataframe of marks and then run the L est function, which requires the data to be pooled. I have found examples of subsetting marks of single point patterns and examples of subsetting hyperframes based on columns of a hyperframe, but I have not seen examples subsetting point patterns of a hyperframe calling levels of a factor from a dataframe with multiple marks. Any guidance would be much appreciated.

I can subset the hyperframe by plot level factors, lets say a,b,and c vegetation types, then run a Lest for each plot, pool the outputs based on the vegetation type, and graph the pooled Lest (pg. 684 of Baddeley et al. 2015 provides a helpful example).

I fail however to subset the point patterns of the hyperframe based on specific marks of the dataframe. I'm not sure if the structure of my data causes problems, so I've included below, or if I'm just confused with the code that subsets marks of a dataframe associated with multiple point patterns of a hyperframe (R novice here. The lists within list gets confusing).

Data construction:

    z.list <- mapply(as.ppp, X = df.list, W = window.list, SIMPLIFY=FALSE) 
     #df.list contains x,y coordinates, followed by columns of point specific 
      #data. 
    h <- hyperframe(X=z.list)
    H <- cbind.hyperframe(h, plot.df1)#combine the point pattern and marks 
     #with plot level data

Data Structure:

     str(H)
    'hyperframe':    93 rows and 14 columns 
     $ X : objects of class ppp 
     $ PLOTID : factor 0102U001 0104U001 0104U002 ... 
     $ Group1 : integer 1 2 1 ... 
     $ Group2 : numeric 2.0 2.5 2.0 ... 

    str(H[1,]$X) #str of the ppp of the hyperframes first row
    List of 1
    $ X:List of 6
    ..$ window    :List of 5
    .. ..$ type  : chr "polygonal"
    .. ..$ xrange: num [1:2] 516441 516503
    .. ..$ yrange: num [1:2] 3382698 3382804
    .. ..$ bdry  :List of 1
    .. .. ..$ :List of 2
    .. .. .. ..$ x: num [1:4] 516503 516502 516441 516442
    .. .. .. ..$ y: num [1:4] 3382698 3382804 3382804 3382698
    .. ..$ units :List of 3
    .. .. ..$ singular  : chr "metre"
    .. .. ..$ plural    : chr "metres"
    .. .. ..$ multiplier: num 1
    .. .. ..- attr(*, "class")= chr "unitname"
    .. ..- attr(*, "class")= chr "owin"
    ..$ n         : int 107
    ..$ x         : num [1:107] 516501 516473 516470 516474 516474 ...
    ..$ y         : num [1:107] 3382801 3382723 3382726 3382734 3382732 ...
    ..$ markformat: chr "dataframe"
    ..$ marks     :'data.frame':    107 obs. of  3 variables:
    .. ..$ DBH_Class: Factor w/ 16 levels "11.25","13.75",..: 7 5 13 12 8 4 9 
    .. ..$ Ingrowth : Factor w/ 7 levels "DD","I_DD","I_LD_MY",..: 7 6 6 7 
    .. ..$ PlotID    : Factor w/ 93 levels "0102U001","0104U001",..: 1 1 1 1 
    ..- attr(*, "class")= chr "ppp"
    - attr(*, "class")= chr [1:5] "ppplist" "solist" "anylist" "listof" ...

The above seems correct to me, but I notice that although the marks print with the str function, is.multipoint outputs as FALSE. Not sure if this is part of my problem. The following works great for plot level factors located in rows of the hyperframe.

    H$L <- with(H, Lest((X),rmax=40))
    L.VT.split <- split(H$L, H$VEG_TYPE) #plot level factor
    L.VT.pool <- anylapply(L.VT.split, pool)
    plot(L.VT.pool,cbind(pooliso, pooltheo, hiiso,loiso)-r~r,
      shade=c("hiiso","loiso"),equal.scales=TRUE, main='')

But how does one perform the same operation using marks from a dataframe?

Upvotes: 0

Views: 424

Answers (1)

Ege Rubak
Ege Rubak

Reputation: 4507


I'm not sure I quite understand the question, but I will try to provide some useful hints anyway...

For each row in H you have a point pattern which contains mark information in a data.frame (three columns called DBH_Class, Ingrowth and PlotID). Here are some fake data with that structure:

library(spatstat)
set.seed(42)
df1 <- data.frame(x = runif(3), y = runif(3), DBH_Class = factor(1:3),
                  Ingrowth = LETTERS[1:3], PlotID = letters[1:3])
X1 <- as.ppp(df1, W = square(1))
df2 <- data.frame(x = runif(3), y = runif(3), DBH_Class = factor(1:3),
                  Ingrowth = LETTERS[1:3], PlotID = letters[1:3])
X2 <- as.ppp(df2, W = square(1))
H <- hyperframe(X = list(X1 = X1, X2 = X2))
H
#> Hyperframe:
#>       X
#> 1 (ppp)
#> 2 (ppp)
plot(H$X, which.marks = "Ingrowth")

To subset an individual point pattern by a specific mark (Ingrowth in this example) use subset:

X1B <- subset(X1, Ingrowth == "B")

Same thing for each pattern in the column X within H:

H$XB <- with(H, subset(X, Ingrowth == "B"))
H
#> Hyperframe:
#>       X    XB
#> 1 (ppp) (ppp)
#> 2 (ppp) (ppp)
plot(H$XB, which.marks = "Ingrowth")

Upvotes: 1

Related Questions