Skip to content

Instantly share code, notes, and snippets.

@GeorgeOduor
Last active May 22, 2022 09:16
Show Gist options
  • Save GeorgeOduor/aee4559e01db7fe8878ec9a3aed3fbc0 to your computer and use it in GitHub Desktop.
Save GeorgeOduor/aee4559e01db7fe8878ec9a3aed3fbc0 to your computer and use it in GitHub Desktop.
rm(list = ls())
library(tidyverse)
library(R6)
# datastes ------
set.seed(1)
population1 = tibble(x = rnorm(20.00, 20, 5),
y = rnorm(20.00, 15, 5),)
population2 = tibble(x = rpois(3231, 50),
y = rnorm(3231, 45),)
population3 = tibble(x = runif(8512, 20, 100),
y = runif(8512, 18, 155))
proj = R6Class(
public = list(
x = NA,
y = NA,
n = NA,
initialize = function(x, y, n) {
self$x = x
self$y = y
self$n = n
},
pop_var = function(x) {
x_xbar = (x - mean(x)) ^ 2 %>% sum()
res = x_xbar / (length(x) - 1)
return(res)
},
beta1 = function(S_x) {
x = self$x
num = (sum((x - mean(x)) ^ 3) * length(x))
den = ((length(x) - 1) * (length(x) - 2) * S_x ^ 3)
beta = num / den
return(beta)
},
beta2 = function(S_x) {
x = self$x
num1 = length(x) * (length(x) + 1) * sum((x - mean(x)) ^ 4)
den1 = ((length(x) - 1) * (length(x) - 2) * (length(x) - 3)) * S_x ^
4
num2 = 3 * (length(x) - 1) ^ 2
den2 = ((length(x) - 2) * (length(x) - 3))
beta = (num1 / den1)#-(num2/den2)
return(beta)
},
t_m = function() {
quartiles = quantile(self$x) %>%
as_tibble() %>%
rowid_to_column() %>%
filter(rowid %in% 2:4) %>%
mutate(rowid = paste0("Q_", rowid - 1))
Q1 = quartiles %>% filter(rowid == "Q_1") %>% pull(value)
Q2 = quartiles %>% filter(rowid == "Q_2") %>% pull(value)
Q3 = quartiles %>% filter(rowid == "Q_3") %>% pull(value)
T_m = (Q1 + 2 * Q2 + Q3) / 4
return(T_m)
},
M_r = function() {
x = self$x
mr = (min(x) + max(x)) / 2
return(mr)
},
QD = function() {
quartiles = quantile(self$x) %>%
as_tibble() %>%
rowid_to_column() %>%
filter(rowid %in% 2:4) %>%
mutate(rowid = paste0("Q_", rowid - 1))
Q1 = quartiles %>% filter(rowid == "Q_1") %>% pull(value)
Q2 = quartiles %>% filter(rowid == "Q_2") %>% pull(value)
Q3 = quartiles %>% filter(rowid == "Q_3") %>% pull(value)
qd = (Q3 - Q1) / 2
return(qd)
},
G = function() {
N = length(self$x)
# sum loop
tot = list()
for (i in 1:N) {
rec = (((2 * i) - N - 1) / (2 * N)) * self$x[i]
tot = append(tot, rec)
}
est = (4 / (N - 1)) * sum(unlist(tot))
return(est)
},
Sp_w = function() {
N = length(self$x)
tot = 0
for (i in 1:N) {
rec = (2 * i - N - 1) * self$x[i]
tot= rec + tot
}
res = (sqrt(pi)/(N^2))*tot
return(res)
},
estimates = function() {
out = tibble(
N = length(self$x),
n = self$n,
Y_bar = mean(self$y),
X_bar = mean(self$x),
S_y = sqrt(self$pop_var(x = self$y)),
C_y = sqrt(self$pop_var(x = self$y)) / mean(self$y),
S_x = sqrt(self$pop_var(x = self$x)),
C_x = sqrt(self$pop_var(x = self$x)) / mean(self$x),
beta1 = self$beta1(S_x),
beta2 = self$beta2(S_x),
M_d = median(self$x),
T_m = self$t_m(),
M_r = self$M_r(),
QD = self$QD(),
G = self$G(),
Sp_w = self$Sp_w()
) %>% t() %>%
as_tibble(rownames = "Parameter")
return(out)
}
)
)
# x = c(9, 10, 12, 15, 25, 30, 35, 40, 50, 50, 50, 100, 100, 150, 250)
# pop1$Sp_w() %>% unlist()
pop1 = proj$new(x = population1$x, y = population1$y, n = 20)
pop2 = proj$new(x = population2$x, y = population2$y, n = 20)
pop3 = proj$new(x = population3$x, y = population3$y, n = 50)
results = pop1$estimates() %>%
left_join(pop2$estimates(), by = 'Parameter') %>%
left_join(pop3$estimates(), by = 'Parameter') %>%
mutate_if(is.numeric, round, 4)
colnames(results) = c('Parameter', paste0("Population_", 1:3))
writexl::write_xlsx(results, "results.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment