I want to generate a raster surface (cost surface) based only on distance from center of extent where the values in the raster cells generate a Weibull distribution. I am interested specifically in the Weibull distribution, but the approach should be flexible enough to be modified to use with other distributions.
Here is some example code:
library(raster)
mat = matrix(data = rep(0,400),nrow=20,ncol=20)
rast = raster(mat,xmn=-10,xmx=10,ymn = -10,ymx=10)
crs(rast) = '+proj=utm +zone=12 +datum=WGS84'
plot(rast)
distFromPt = 1/raster::distanceFromPoints(rast, c(0,0))
plot(distrFromPt)
I want to be able to sample from the distrFromPt, such that the values of distrFromPt represent the probability that the cell would be chosen. For example, if the shape and scale parameters were both 1, then sampling 1000 random points should generate a distance histogram that looked like a weibull distribution.
hist(rweibull(1000,1,1),breaks=50)
I think the answer is somewhere in turning the values of distFromPt into quantiles, but just can't quite think how to do it.
Update:
Based on @Spacedman recommendations and another posting by @ Spacedman here, I have generated the following code:
library(raster)
mat = matrix(data = rep(0,400),nrow=20,ncol=20)
rast = raster(mat,xmn=-10,xmx=10,ymn = -10,ymx=10)
crs(rast) = '+proj=utm +zone=12 +datum=WGS84'
plot(rast)
distFromPt = 1/raster::distanceFromPoints(rast, c(0,0))
plot(distFromPt)
D = values(distFromPt)
Zw = qweibull(seq(0,1,len = length(rast)),1,1)
ZD = Zw[rank(D)]
values(rast) = ZD/sum(ZD)
hs = res(rast)/2
ptscell = sample(1:length(rast), 1000, prob=rast[], replace=TRUE)
centres = xyFromCell(rast,ptscell)
df = cbind(runif(nrow(centres),centres[,1]-hs[1],centres[,1]+hs[1]),
runif(nrow(centres),centres[,2]-hs[2],centres[,2]+hs[2]))
pts = SpatialPointsDataFrame(coords = df,proj4string = crs(rast),
data = as.data.frame(df))
pts$Dist = pointDistance(pts,c(0,0),lonlat = FALSE, type = "Euclidean")
However, when you map the histogram of the distances, it doesn't look like I am able to recover a Weibull distribution.
hist(pts$Dist)
I think that this is because, even though the distant cells have a low probability of being selected, there are so many of them they "overwhelm" the cells close to the centroid that have a high probability, but low number of instances.
distFromPt
raster into something with a distribution broadly similar to a Weibull(a,b)? So any general method for transforming data from its empirical distribution to any other distribution would do?