How can I edit this code to determine the accuracy of data? My employer has asked me to perform a few analysis on a data about wood piles which contains their diameter and bark thickness.
I am a beginner in R and started with some basic descriptive analysis and now he wants me to create a model which kind of increases in accuracy as I give more number of samples to it. He meant to say that I take the mean of one bark thickness, then two, three and so on increasing the number of entries every time and compare this mean with the rest of the data and plot it? In the end, he is expecting a graph that shows an increase in accuracy of sample mean to total mean as we increase the number of samples.
As of now, the goal is to figure out how many samples of woods in a wood pile, we need to take in order to estimate the total woodpile volume.
I kind of get the idea but can't implement it without studying about which method exactly he is talking about. I wonder if there is any kind of particular analysis that I should look in to which can help me with this data.
This is a code I gotten till now, but it doesn't do exactly what I want.
library(ggplot2)
size_n <- 219
y <- Wood_pile$DUB 
df <- data.frame(
  x = seq(1:size_n), 
  y = y, 
  cu_y = cumsum(y),
  avg = mean(y)) %>%
  transform(cu_mean = cu_y/x) 

ggplot(df, aes(x, cu_mean)) + geom_point() + geom_line(aes(y=avg))

This is what my data looks like :
DOB - diameter with bark, DUB - diameter without bark
What can I add in this code to give a graph of accuracy?

Your code works fine, you need to ensure couple of things:

*

*You are drawing $n$ samples randomly from the populations


*Samples are drawn independently (with replacement), s.t., they form i.i.d. random variables.


*It theoretically guarantees that your sample mean $\bar{X_n}$ will converge to population mean $\mu=E[X]$, both in probability and almost surely, by WLLN ($\bar{X_n} \overset{p}{\rightarrow} \mu$) and SLLN ($\bar{X_n} \overset{a.s.}{\rightarrow} \mu$) respectively, as we draw more and more samples ($n\to \infty$).


*Additionally, you can measure the difference (error) in between sample and population mean with some metric as Squared Error / Absolute Error and show how the error converges to zero) as we increase the sample size, as shown below, generated with some synthetic data.
library(ggplot2)
# generate some data
N <- 2000 # population size
Wood_pile <- data.frame(ID = 1:N, DOB = runif(N, 15, 25), DUB = runif(N, 15, 25), Bark_thickness=runif(N))
y <- Wood_pile$DUB 

# draw samples
nsamples <- 200 # sample size
indices <- sample(1:N, nsamples, replace=TRUE) # draw i.i.d. samples
y <- y[indices]
df <- data.frame(
   x = seq(1:nsamples), 
   y = y, 
   cu_y = cumsum(y),
   avg = mean(y)) %>%
   transform(cu_mean = cu_y/x) 

# plot convergence
ggplot(df, aes(x, cu_mean)) + 
   geom_point(aes(color='sample mean')) + 
   geom_line(aes(y=avg, col='population mean'), lwd=2) + 
   xlab('#samples') +
   ylab('mean')


# compute errors
df$se <- mean((df$avg - df$cu_mean)^2)
df$ae <- abs(df$avg - df$cu_mean)

# plot errors
ggplot(df, aes(x, ae)) + geom_line(col='red', lwd=2) + xlab('#samples') + ylab('Absolute Error')
# plot SE now



*

*As we know, sample mean $\bar{X_n}$ is an unbiased estimator of the population mean ($\mu$). i.e., $E[\bar{X_n}] = \mu$ and you may want to show that if you draw random samples (with replacement) multiple times and compute the mean of the sample means (consider the sampling distribution), it's going to estimate the population mean more closely as you increase your sample size, this is by CLT, since $\frac{\bar{X_n}-\mu}{\sigma/\sqrt{n}}\overset{D}{\rightarrow}\mathcal{N}(0,1)$, as $n\to\infty$.
mu_y <- mean(y)
sigma_y <- sd(y)
y_bars <- c()
for (i in 1:1000) {
  y_s <- sample(y, nsamples, replace=TRUE)
  y_bars <- c(y_bars, mean(y_s))
}

ggplot() + geom_histogram(aes((y_bars-mu_y)/(sigma_y/sqrt(nsamples)), 
           y = ..density..)) + 
geom_line(aes(seq(-4,4, 0.01), dnorm(seq(-4,4, 0.01)), color='N(0,1)'), lwd=2) + xlab('(ybar-μ)/(σ/√n)') 


