# file: prg5.R # To use it, type "source("prg5.R")" after the prompt # # Solution to Ex 5.1 show51 <- function () { pop <- function (x) {if (x == 1) 2 else if (x == 2) 3 else 5} summean <- function () {m <- 0; for (i in 1:3) for (j in 1:3) for (k in 1:3) m <- m + pop(i)+pop(j)+p(k); m/27} print("The mean of the sum statistic is:") print(summean()) sumvar <- function () {m <- 0; u <- summean (); for (i in 1:3) for (j in 1:3) for (k in 1:3) {m <- m + (pop(i)+pop(j)+p(k) - u) ** 2}; m/27} print("The variance of the sum statistic is:") print(sumvar()) medmean <- function () {m <- 0; for (i in 1:3) for (j in 1:3) for (k in 1:3) {s <- sort(c(pop(i),pop(j),pop(k))); m <- m + s[2]}; m/27} print("The mean of the media statistic is:") print(medmean()) medvar <- function () {m <- 0; u <- medmean (); for (i in 1:3) for (j in 1:3) for (k in 1:3) {s <- sort(c(pop(i),pop(j),pop(k))); m <- m + (s[2] - u) ** 2}; m/27} print("The variance of the median statistic is:") print(medvar()) } # Solution to Ex 5.2 show52 <- function () { th <- function (x) {k <- sum(x); n <- length (x); .5 ** n/((.8 ** k) * (.2 ** (n- k)))} oma <- function (n) th(seq(c(0,1),c(n,n))) tha <- oma(c(5,10,15,20)) print("The threshold on equal numbers of 0 and 1 for length 10,20,30,40:") print(tha) omb <- function (n) th(seq(c(0,1),c(n,2 * n))) thb <- omb(c(5,10,15,20)) print("The threshold on twice the numbers of 1 than 0 for length 15,30,45,60:") print(thb) omc <- function (n) th(seq(c(0,1),c(2 * n,n))) thc <- omc(c(5,10,15,20)) print("The threshold on twice the numbers of 0 than 1 for length 15,30,45,60:") print(thb)} # Solution to Ex 5.3 show53 <- function () {v <- c(2, 3, 5) arr <- outer(v,outer(v,v,"+"),"+") print("The mean is:") mean(as.vector(arr)) print("The variance is:") var(arr)}