Standard distance simulations
library(haffutils)
library(ggplot2)
library(ggforce)
## assign values to x
set.seed(11)
x <- rnorm(10000)
## assign values to y
set.seed(12)
y <- rnorm(10000)
df <- data.frame(x=x, y=y)
# visualize
pretty_dens(x)
pretty_dens(y)
## percentage of x coordinates between -1 and 1
length(x[x<1 & x>-1])/length(x)
## percentage of x coordinates between -2 and 2
length(x[x<2 & x>-2])/length(x)
## percentage of x coordinates between -3 and 3
length(x[x<3 & x>-3])/length(x)
## percentage of y coordinates between -1 and 1
length(y[y<1 & y>-1])/length(y)
## percentage of y coordinates between -2 and 2
length(y[y<2 & y>-2])/length(y)
## percentage of y coordinates between -3 and 3
length(y[y<3 & y>-3])/length(y)
## create function for standard distance
st_dist <- function(x, y) {
sqrt(sum(((x - mean(x))^2)/length(x)) + (sum((y - mean(y))^2)/length(y)))
}
## plot points
pts_plotted <- ggplot(df, aes(x=x, y=y)) +
geom_point(size = 0.3) +
coord_fixed()
pts_plotted
## draw standard distances as circles
pts_plotted +
## one standard distance
geom_circle(aes(x0 = mean(x),
y0 = mean(y),
r = st_dist(x,y))) +
## two standard distances
geom_circle(aes(x0 = mean(x),
y0 = mean(y),
r = 2*st_dist(x,y))) +
## three standard distances
geom_circle(aes(x0 = mean(x),
y0 = mean(y),
r = 3*st_dist(x,y)))
## calculate distance from mean center to each point
df$dist <- sqrt((mean(x) - x)^2 + (mean(y) - y)^2)
## percentage of points falling within one standard distance of the mean center
nrow(df[df$dist < 1 * st_dist(x, y),])/nrow(df)
## percentage of points falling within two standard distances of the mean center
nrow(df[df$dist< 2 * st_dist(x, y),])/nrow(df)
## percentage of points falling within three standard distances of the mean center
nrow(df[df$dist < 3 * st_dist(x,y),])/nrow(df)