library(knitr)
library(bmiproj)

Recurrence Relationship for Mean

$$\mu_n = \mu_{n - 1} + \frac{x_n - \mu_{n-1}}{n}$$

From the formula above, the old mean is $\mu_{n-1}$ and the new data is $x_n$.

An example code from the function new_mean

y <- new_mean(10, 12, 4)
y

Recurrence Relationship for Sample Variance

$$s_n^2 = \frac{n- 2}{n - 1}s_{n - 1}^2 + \frac{(x_n - \mu_{n-1})^2}{n} $$

From the formula above, the old sample variance is $s^2_{n - 1}$, the new data is $x_n$ and the old mean is $\mu_{n - 1}$.

An example code from the function newvar

x <- newvar(5, 4, 6, 5)
x

Odds Ratio and Confidence Interval of Odds Ratio

data <- array(c("a", "c", "b", "d"), c(2, 2))
colnames(data) <- c("Risk", "No Risk")
rownames(data) <- c("Treatment", "Control")
kable(data)

The 2x2 contigency table above shows how the data could be used in function odds.ratio. The CI parameter controls the two-sided confidence interval.

An example of the function odds.ratio

z <- oddsratio(c(50, 40, 40, 60), CI = 0.90)
z

Crowfly distance between two points on earth (using lattitude and longitude)

The lat1 and lat2 can only take any values between -90 and 90. On the other hand, long1 and long2 can only take values between -180 and 180.

An example of the function crowfly_dist

crowfly_dist <- function (long1, lat1, long2, lat2){
  stopifnot(sapply(c(long1, lat1, long2, lat2), is.numeric))
  stopifnot(all(c(long1, long2) < 180 & c(long1, long2) > -180))
  stopifnot(all(c(lat1, lat2) < 90 & c(lat1, lat2) > -90))
  rad <- pi / 180
  a1 <- lat1 * rad
  a2 <- long1 * rad
  b1 <- lat2 * rad
  b2 <- long2 * rad
  dlon <- b2 - a2
  dlat <- b1 - a1
  a <- (sin(dlat / 2))^2 + cos(a1) * cos(b1) * (sin(dlon / 2))^2
  c <- 2 * atan2(sqrt(a), sqrt(1 - a))
  R <- 6378.145
  d <- R * c
  return(d / 1.60934)
}
distance <- crowfly_dist(-50, 20, 90, -80)
distance


thevaa/bmiproj documentation built on May 31, 2019, 9:51 a.m.