Creating a point distance component to a monte carlo simulation function in R

Viewed 32

I am attempting to do some Monte Carlo simulations, where I have a population of 325 samples in a field. I want to create a list of composite samples (samples consisting of multiple subsamples) from the dataset, while increasing sample size, repeated 100 times. I have created the function that will do so, and have supplied that below in the code.

##Create an example data set
# x and y are coordinates
x <- c(1:100)
y <- rev(c(1:100))
## z and w are soil test values
set.seed(2345)
z <- rnorm(100,mean=50, sd=10)
set.seed(2345)
w <- rnorm(100, mean=75, sd=5)
data <- data.frame(x, y, z, w)

##Initialize list
data.step.sim.list <- list()
## Code that increases sample size
for(i in seq_len(nrow(data))){
  thisdat <- replicate(100,data[sample(1:nrow(data), size=i, replace = F),], simplify = F)
  data.step.sim.list[[i]] <- thisdat
}

The product becomes a list n long (n being length of dataset), with each list consisting of a list of 100 dataframes (100 coming from 100 replications) that are length 1:n length long. I have x and y data for each sample as well, and want to stipulate that each subsample collected would be at least 'm' meters from the other samples. I have created a function that will calculate each distance seen below. I cannot find a way to implement this into my current code. Would anyone know how to do this?

#function to compute distances
calc.dist <- function(x1, y1, x2, y2) {
  d <- sqrt(((x2 - x1)^2) + ((y2 - y1)^2))
  
  return(d)
} #end function calc.dist
0 Answers
Related