简体   繁体   中英

Error: x,y coords given but no window specified (spatstat)

I am generating a landscape pattern that evolves over time. The problem with the code is that I have clearly defined a window for the object bringing up the error but the window is not being recognised. I also do not see how any points are falling outside of the window, or how that would make a difference.

library(spatstat)
library(dplyr)

# Define the window
win <- owin(c(0, 100), c(0, 100))

# Define the point cluster
cluster1 <- rMatClust(kappa = 0.0005, scale = 0.1, mu = 20, 
                      win = win, center = c(5,5))

# define the spread of the points
spread_rate <- 1
new_nests_per_year<-5
years<-10

# Plot the initial cluster
plot(win, main = "Initial cluster")
points(cluster1, pch = 20, col = "red")
newpoints<-list()
# Loop for n years
for (i in 1:years) {
  # Generate new points that spread from the cluster
  newpoints[[1]] <-rnorm(new_nests_per_year, mean = centroid.owin(cluster1)$y, sd = spread_rate)
  newpoints[[2]] <-rnorm(new_nests_per_year, mean = centroid.owin(cluster1)$x, sd = spread_rate)
  
  # Convert the list to a data frame
  newpoints_df <- data.frame(newpoints)
  # Rename the columns of the data frame
  colnames(newpoints_df) <- c("x", "y")
  # Combine the new points with the existing points
  cluster1_df <- data.frame(cluster1)
  newtotaldf<-bind_rows(cluster1_df,newpoints_df)
  cluster1<-as.ppp(newtotaldf, x = newtotaldf$x, y = newtotaldf$y,
                   window = win)
  # Plot the updated cluster
  plot(win, main = paste("Cluster after year", i))
  points(cluster1, pch = 20, col = "red")
}

However, when I run line:

 cluster1<-as.ppp(newtotaldf, x = newtotaldf$x, y = newtotaldf$y,
                   window = win)

I recieve the error:

Error: x,y coords given but no window specified

Why would this be the case?

In your code, if you use the command W = win it should solve the issue. I also believe you can simplify the command without specifying x and y :

## ...[previous code]...

cluster1 <- as.ppp(newtotaldf, W = win)

plot(win)
points(cluster1, pch = 20, col = "red")

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM