0% found this document useful (0 votes)
9 views2 pages

Stat2a Code

The document contains R functions for computing statistical measures for both ungrouped and grouped data, including mean, median, mode, variance, standard deviation, and nth tiles. It provides examples of how to use these functions with specific datasets. Additionally, it lists student names, indicating potential contributors or participants in the work.

Uploaded by

araminapacheco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Stat2a Code

The document contains R functions for computing statistical measures for both ungrouped and grouped data, including mean, median, mode, variance, standard deviation, and nth tiles. It provides examples of how to use these functions with specific datasets. Additionally, it lists student names, indicating potential contributors or participants in the work.

Uploaded by

araminapacheco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# ungrouped data statistics computation

data_mean <- function(arr) {


num <- 0
len <- length(arr)
for (i in arr) {
num <- num + i
}
return (num / len)
}
data_median <- function(arr) {
arr_2 <- sort(arr)
len <- length(arr)
if (len %% 2 == 1) {
get <- len %/% 2
return (arr_2[get + 1])
} else {
mid1 <- len / 2
mid2 <- mid1 + 1
return ((arr_2[mid1] + arr_2[mid2]) / 2)
}
}
data_mode <- function(arr) {
freq <- table(arr)
mode_val <- [Link](names(freq)[freq == max(freq)])
return (mode_val)
}
data_variance <- function(arr) {
sum_val <- 0
freq <- 0
len <- length(arr)
d_mean <- data_mean(arr)
for (i in arr) {
sum_val <- sum_val + (i - d_mean)^2
}
return (sum_val / len)
}
data_stddev <- function(arr) {
return (sqrt(data_variance(arr)))
}
data_nthtile <- function(arr, nth, type) {
arr_2 <- sort(arr)
val <- (nth / type) * (length(arr) + 1)
inter <- val - floor(val)
n1 <- arr_2[floor(val)]
n2 <- arr_2[floor(val) + 1]
return (n1 + (inter * (n2 - n1)))
}
arr <- c(92, 92, 89, 88, 81, 91, 93, 90, 95, 87)
data_mean(arr) # or just do mean(arr)
data_median(arr) # might as well be median(arr)
data_mode(arr) # mode here returns its data type, so it is hardcoded here
data_variance(arr) # var(arr) also exists here
data_stddev(arr) # sd(arr) is even here as well
data_nthtile(arr, 3, 4) # quantiles(arr, args) are also built-in here
data_nthtile(arr, 2, 10) # but works differently for when the
data_nthtile(arr, 25, 100) # required is n-tile

# grouped data statistics computation


group_mean <- function(arr) {
sum_val <- 0
freq <- 0
for (i in 1:nrow(arr)) {
temp <- (arr[i, 1] + arr[i, 2]) / 2
sum_val <- sum_val + (arr[i, 3] * temp)
freq <- freq + arr[i, 3]
}
return (sum_val / freq)
}
group_median <- function(arr) {
cf <- cumsum(arr[3])
n <- sum(arr[3])
mi_ind <- which(cf >= n / 2)[1]
L <- arr[mi_ind, 2]
freq <- ifelse(mi_ind == 1, 0, cf[mi_ind - 1])
f_med <- arr[mi_ind, 3]
h <- arr[2, 1] - arr[1, 1] + 1
return (L + ((((n/2) - freq) / f_med) * h))
}
group_mode <- function(arr) {
# get index of the modal class or its freq
mod_ind <- [Link](arr[3])
l_modal <- arr[mod_ind, 2]
f_m <- arr[mod_ind, 3]
f1 <- ifelse(mod_ind == 1, 0, arr[mod_ind - 1, 3])
f2 <- ifelse(mod_ind == nrow(arr), 0, arr[mod_ind + 1, 3])
h <- arr[2, 1] - arr[1, 1] + 1
return (l_modal + ((f_m - f1) / (f_m - f1) + (f_m - f2)) * h)
}
group_variance <- function(arr) {
sum_val <- 0
freq <- 0
g_mu <- group_mean(arr)
for (i in 1:nrow(arr)) {
temp <- (arr[i, 1] + arr[i, 2]) / 2
sum_val <- sum_val + (arr[i, 3] * (temp - g_mu)^2)
freq <- freq + arr[i, 3]
}
return (sum_val / freq)
}
group_stddev <- function(arr) {
return (sqrt(group_variance(arr)))
}
group_nthtile <- function(arr, nth, type) {
freq <- sum(arr[3])
pos <- (nth * freq) / type
cf <- cumsum(arr[3])
index <- which(cf >= pos)[1]
l_qnt <- arr[index, 2]
F_qnt <- ifelse(index == 1, 0, cf[index - 1])
f_qnt <- arr[index, 3]
h <- arr[2, 1] - arr[1, 1] + 1
return (l_qnt + ((pos - F_qnt) / f_qnt) * h)
}
class_data <- [Link] (
up_class = c(20, 25, 30, 35, 40, 45),
lw_class = c(16, 21, 26, 31, 36, 41),
freq = c(2, 7, 14, 8, 8, 1)
)
group_mean(class_data)
group_median(class_data)
group_mode(class_data)
group_variance(class_data)
group_stddev(class_data)
group_nthtile(class_data, 3, 4)
group_nthtile(class_data, 2, 10)
group_nthtile(class_data, 25, 100)

Locsin, Paul Aljon B.


Vallejos, Allein Dion A.
Briones, Dwight Kirby
Quilon, Jommel Sean B.
Lanting, Neil Gabriel S. (Absent)

You might also like