← Back to context Comment by mi_lk 5 hours ago Can you rewrite some of those snippets in standard R w/o Tidyverse? Curious what it would look like 2 comments mi_lk Reply lottin 2 hours ago I didn't rewrite the whole thing. But here's the first part. It uses the `histogram` function from the lattice package. population_data <- data.frame( uniform = runif(10000, min = -20, max = 20), normal = rnorm(10000, mean = 0, sd = 4), binomial = rbinom(10000, size = 1, prob = .5), beta = rbeta(10000, shape1 = .9, shape2 = .5), exponential = rexp(10000, .4), chisquare = rchisq(10000, df = 2) ) histogram(~ values|ind, stack(population_data), layout = c(6, 1), scales = list(x = list(relation="free")), breaks = NULL) take_random_sample_mean <- function(data, sample_size) { x <- sample(data, sample_size) c(mean = mean(x), sd = sqrt(var(x))) } sample_statistics <- replicate(20000, sapply(population_data, take_random_sample_mean, 60)) sample_mean <- as.data.frame(t(sample_statistics["mean", , ])) sample_sd <- as.data.frame(t(sample_statistics["sd", , ])) histogram(sample_mean[["uniform"]]) histogram(sample_mean[["binomial"]]) histogram(~values|ind, stack(sample_mean), layout = c(6, 1), scales = list(x = list(relation="free")), breaks = NULL) apwheele 3 hours ago I mean, for the main simulation I would do it like this: set.seed(10) n <- 10000; samp_size <- 60 df <- data.frame( uniform = runif(n, min = -20, max = 20), normal = rnorm(n, mean = 0, sd = 4), binomial = rbinom(n, size = 1, prob = .5), beta = rbeta(n, shape1 = .9, shape2 = .5), exponential = rexp(n, .4), chisquare = rchisq(n, df = 2) ) sf <- function(df,samp_size){ sdf <- df[sample.int(nrow(df),samp_size),] colMeans(sdf) } sim <- t(replicate(20000,sf(df,samp_size))) I am old, so I do not like tidyverse either -- I can concede it is of personal preference though. (Personally do not agree with the lattice vs ggplot comment for example.)
lottin 2 hours ago I didn't rewrite the whole thing. But here's the first part. It uses the `histogram` function from the lattice package. population_data <- data.frame( uniform = runif(10000, min = -20, max = 20), normal = rnorm(10000, mean = 0, sd = 4), binomial = rbinom(10000, size = 1, prob = .5), beta = rbeta(10000, shape1 = .9, shape2 = .5), exponential = rexp(10000, .4), chisquare = rchisq(10000, df = 2) ) histogram(~ values|ind, stack(population_data), layout = c(6, 1), scales = list(x = list(relation="free")), breaks = NULL) take_random_sample_mean <- function(data, sample_size) { x <- sample(data, sample_size) c(mean = mean(x), sd = sqrt(var(x))) } sample_statistics <- replicate(20000, sapply(population_data, take_random_sample_mean, 60)) sample_mean <- as.data.frame(t(sample_statistics["mean", , ])) sample_sd <- as.data.frame(t(sample_statistics["sd", , ])) histogram(sample_mean[["uniform"]]) histogram(sample_mean[["binomial"]]) histogram(~values|ind, stack(sample_mean), layout = c(6, 1), scales = list(x = list(relation="free")), breaks = NULL)
apwheele 3 hours ago I mean, for the main simulation I would do it like this: set.seed(10) n <- 10000; samp_size <- 60 df <- data.frame( uniform = runif(n, min = -20, max = 20), normal = rnorm(n, mean = 0, sd = 4), binomial = rbinom(n, size = 1, prob = .5), beta = rbeta(n, shape1 = .9, shape2 = .5), exponential = rexp(n, .4), chisquare = rchisq(n, df = 2) ) sf <- function(df,samp_size){ sdf <- df[sample.int(nrow(df),samp_size),] colMeans(sdf) } sim <- t(replicate(20000,sf(df,samp_size))) I am old, so I do not like tidyverse either -- I can concede it is of personal preference though. (Personally do not agree with the lattice vs ggplot comment for example.)
I didn't rewrite the whole thing. But here's the first part. It uses the `histogram` function from the lattice package.
I mean, for the main simulation I would do it like this:
I am old, so I do not like tidyverse either -- I can concede it is of personal preference though. (Personally do not agree with the lattice vs ggplot comment for example.)