Data/Sample generated from Normal Distribution
set.seed(1234)
true_mean <- 0
true_var <- 1
n <- 10
reps <- 10
cat("For a normal distribution with a mean of", true_mean,
"and a variance of", true_var,
"\nWith n =", n, "we should obtain a sample mean of", true_mean,
"and a sample variance of", true_var/n,"\n")
## For a normal distribution with a mean of 0 and a variance of 1
## With n = 10 we should obtain a sample mean of 0 and a sample variance of 0.1
x_vect <- vector(mode = "numeric", length = reps)
for (i in 1:length(x_vect)) {
x_temp <- rnorm(n = n)
x_vect[i] <- mean(x_temp)
}
x_bar <- mean(x_vect)
var_xbar <- var(x_vect)
cat("\nResults:",
"\nSample mean:", x_bar,
"\nSample variance:", var_xbar,
"\nBlue Line = Actual density curve ",
"\nRed Line = Normal density curve with true mean & expected sample variance")
##
## Results:
## Sample mean: -0.1567617
## Sample variance: 0.2026328
## Blue Line = Actual density curve
## Red Line = Normal density curve with true mean & expected sample variance
hist(x_vect, freq = F, main = "Histogram of Means of Each Trial", xlab = "Means",
ylim = c(0, max(dnorm(true_mean, mean = true_mean, sd = sqrt(true_var/n)),
max(hist(x_vect, plot = F)$density))))
curve(dnorm(x, mean = true_mean, sd = sqrt(true_var/n)), col = "red", add = T)
lines(density(x_vect), col = "blue")
qqnorm(x_vect)
qqline(x_vect, col = "blue")
Data/sample generated from \(\chi^{2}\) Basic Code
set.seed(1234)
#x<-matrix(rep(0,1000*1000),1000,1000)
t <- 1000
xbar <- rep(0, t)
for (i in 1:t) {
x <- rchisq(1000, df=60)
xbar[i] <- mean(x)
}
#xbar
muxbar <- mean(xbar)
muxbar
## [1] 60.0017
varxbar <- var(xbar)
varxbar
## [1] 0.1280896
sigmasquare <- 2*60
sigmasquaren <- 2*60/1000
sigmasquaren
## [1] 0.12
hist(xbar)
chisq_fn <- function (reps = 1000, n = 1000, df = 1) {
set.seed(1234)
true_mean <- df
true_var <- 2*df
# output
cat("For a chi-square distribution with", df, "degree(s) of freedom,",
"\nThe true mean is", true_mean,
"\nThe true variance is", true_var,
"\nWith n =", n, "we should obtain a sample mean of", true_mean,
"and a sample variance of", true_var/n,"\n")
x_vect <- vector(mode = "numeric", length = reps)
for (i in 1:length(x_vect)) {
x_temp <- rchisq(n, df)
x_vect[i] <- mean(x_temp)
}
x_bar <- mean(x_vect)
var_xbar <- var(x_vect)
##Output of simulation
cat("\nResults:",
"\nSample mean:", x_bar,
"\nSample variance:", var_xbar,
"\nBlue Line = Actual density curve",
"\nRed Line = Normal density curve with true mean & expected sample variance")
##histogram of sampling distribution of sample means
hist(x_vect, freq = F, main = "Sampling distribtution of the observed sample mean", xlab = "Means",
ylim = c(0, max(dnorm(true_mean, mean = true_mean, sd = sqrt(true_var/n)),
max(hist(x_vect, plot = F)$density))))
##adds a normal curve to the histogram
curve(dnorm(x, mean = true_mean, sd = sqrt(true_var/n)), col = "red", add = T)
##adds the density function of the repeated sampling
lines(density(x_vect), col = "blue")
##qqnorm plot of the sampling, with line
qqnorm(x_vect)
qqline(x_vect, col = "blue")
}
Q1<-chisq_fn(reps = 1000, n = 3, df = 3)
## For a chi-square distribution with 3 degree(s) of freedom,
## The true mean is 3
## The true variance is 6
## With n = 3 we should obtain a sample mean of 3 and a sample variance of 2
##
## Results:
## Sample mean: 3.011064
## Sample variance: 2.12823
## Blue Line = Actual density curve
## Red Line = Normal density curve with true mean & expected sample variance
Q1
## NULL
chisq_fn(reps = 10, n = 10, df = 60)
## For a chi-square distribution with 60 degree(s) of freedom,
## The true mean is 60
## The true variance is 120
## With n = 10 we should obtain a sample mean of 60 and a sample variance of 12
##
## Results:
## Sample mean: 60.46305
## Sample variance: 12.27168
## Blue Line = Actual density curve
## Red Line = Normal density curve with true mean & expected sample variance
chisq_fn(reps = 10, n = 10, df = 60)
## For a chi-square distribution with 60 degree(s) of freedom,
## The true mean is 60
## The true variance is 120
## With n = 10 we should obtain a sample mean of 60 and a sample variance of 12
##
## Results:
## Sample mean: 60.46305
## Sample variance: 12.27168
## Blue Line = Actual density curve
## Red Line = Normal density curve with true mean & expected sample variance
chisq_fn(reps = 1000, n = 10, df = 60)
## For a chi-square distribution with 60 degree(s) of freedom,
## The true mean is 60
## The true variance is 120
## With n = 10 we should obtain a sample mean of 60 and a sample variance of 12
##
## Results:
## Sample mean: 60.04534
## Sample variance: 10.80491
## Blue Line = Actual density curve
## Red Line = Normal density curve with true mean & expected sample variance
chisq_fn(reps = 10, n = 100, df = 60)
## For a chi-square distribution with 60 degree(s) of freedom,
## The true mean is 60
## The true variance is 120
## With n = 100 we should obtain a sample mean of 60 and a sample variance of 1.2
##
## Results:
## Sample mean: 60.36378
## Sample variance: 1.81453
## Blue Line = Actual density curve
## Red Line = Normal density curve with true mean & expected sample variance