SM - Lab 4 - R

an anonymous user · January 15, 2022 · R
# ----------------------------
# Question 1                
# ----------------------------


observations <- c(2, 4, 10, 12, 14)

missing_obs_1 <- 8
missing_obs_2 <- 6

observations <- append(observations, missing_obs_1)
observations <- append(observations, missing_obs_2)

paste('Mean:', mean(observations))
paste('Var:', var(observations) * (length(observations) - 1) / length(observations))


# ----------------------------
# Question 2       
# ----------------------------


original_mean = 8
original_stddev = 4
multiplier = 4

new_mean = original_mean * multiplier
new_stddev = original_stddev * multiplier

paste('New Mean:', new_mean)
paste('New StdDev:', new_stddev)

a <- c(1, 2, 3, 4)

paste('Mean:', mean(a))
paste('var:', sqrt(var(a) * 3/4))

a <- c(4, 8, 12, 16)

paste('Mean:', mean(a))
paste('var:', sqrt(var(a) * 3/4))


# ----------------------------
# Question 3                
# ----------------------------



# ----------------------------
# Question 4                
# ----------------------------


marks <- c(15, 25, 35, 45, 55, 65, 75)
grp_a <- c(9, 17, 32, 33, 40, 10, 9)
grp_b <- c(10, 20, 30, 25, 43, 15, 7)

mean_a <- mean(rep(marks, grp_a))
var_a <- var(rep(marks, grp_a)) * ((sum(grp_a) - 1) / sum(grp_a))
stdDev_a <- sqrt(var_a)
cov_a <- stdDev_a / mean_a * 100

mean_b <- mean(rep(marks, grp_b))
var_b <- var(rep(marks, grp_b)) * ((sum(grp_b) - 1) / sum(grp_b))
stdDev_b <- sqrt(var_b)
cov_b <- stdDev_b / mean_b * 100


paste('Covariance of Group A:', cov_a)
paste('Covariance of Group B:', cov_b)


# ----------------------------
# Question 5                
# ----------------------------


weights <- c(36, 41, 46, 51, 56, 61, 66, 71)
students <- c(100, 96, 79, 56, 28, 11, 5, 2)

paste('Mean:', mean(rep(weights, students)))
paste('Var:', var(rep(weights, students)) * (sum(students) - 1) / sum(students))

h = hist(rep(weights, students), xlim = c(30, max(weights) + 10), col = 'blue', right = F)

mp = c(min(h$mids) - (h$mids[2] - h$mids[1]), h$mids, max(h$mids) + (h$mids[2] - h$mids[1]))
freq = c(0, h$counts, 0)

h = hist(rep(weights, students), xlim = c(30, max(weights) + 10), col = 'blue', right = F)
lines(mp, freq, type = 'b', pch = 20, col = 'red', lwd = 3)






Comments

Please sign up or log in to contribute to the discussion.