library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggthemes)
library(ggrepel)
library(effsize)
library(pwrss)
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test
sensor_data <- read.csv("/Users/manojdannana/Desktop/Manoj/IUPUI/class work/intro to statistics/datasets/Occupancy_Estimation.csv")

Hypothesis I

Let’s define an Alternate Hypothesis first

Null Hypothesis: There is no significant difference in the mean light readings between any pair of sensors.

Alternate Hypothesis: At least one pair of sensors has significantly different mean light readings.

This is a Two Tailed Test

Some important statistical assumptions to be made for Hypothesis Testing

  • Alpha Level or Significance Value:
    • Value Chosen: 0.05
    • Reason: Alpha levels are chosen based on the context and based on how low the level of Type-1 error should be. Since, this isn’t a very important or a critical issue I am going with the standard value of 0.05 which is a standard choice that aligns with common statistical practices.
  • Power Level:
    • Value Chosen: 0.08
    • Reason: This is the probability of correctly rejecting the Null Hypothesis. I chose 0.08 because it is a standard choice that aligns with common statistical practices.
  • Minimum Effect Size:
    • Value Chosen: 0.20
    • Reason: This is the the minimum value for effect size which should be considered while comparing two samples of the same population, smaller the effect size smaller the difference between the two sampling distributions. I am going with 0.20 as my minimum effect size as it is a common statistical practices.

Neyman-Pearson Testing

sensor_data$Light_diff = sensor_data$S1_Light - sensor_data$S2_Light
observed_diff <- abs(mean(sensor_data$S1_Light) - mean(sensor_data$S2_Light))
bootstrap <- function (x, func=mean, n_iter=10^4) {
  # empty vector to be filled with values from each iteration
  func_values <- c(NULL)
  
  # we simulate sampling `n_iter` times
  for (i in 1:n_iter) {
    # pull the sample (e.g., a vector or data frame)
    x_sample <- sample_n(x, size = length(x), replace = TRUE)
    
    # add on this iteration's value to the collection
    func_values <- c(func_values, func(x_sample))
  }
  
  return(func_values)
}
diff_in_avg <- function (x_data) {
  light_diff <- mean(x_data$S1_Light) - mean(x_data$S2_Light)
  return(light_diff)
}

diffs_in_avgs <- bootstrap(sensor_data, diff_in_avg, n_iter = 1000)
ggplot() +
  geom_function(xlim = c(-25, 25), 
                fun = function(x) dnorm(x, mean = 0, 
                                        sd = sd(diffs_in_avgs))) +
  geom_vline(mapping = aes(xintercept = observed_diff,
                           color = paste("observed: ",
                                         round(observed_diff)))) +
  labs(title = "Bootstrapped Sampling Distribution of mean Light Difference",
       x = "Difference in Mean Temperatures of S1_Light and S2_Light",
       y = "Probability Density",
       color = "") +
  scale_x_continuous(breaks = seq(-25, 25, 5)) +
  theme_minimal()

cohen.d(d = sensor_data$S1_Light,
        f = sensor_data$S2_Light)
## 
## Cohen's d
## 
## d estimate: -0.009565797 (negligible)
## 95 percent confidence interval:
##       lower       upper 
## -0.03710861  0.01797701

The effect size is less than 0.2 so the difference between the both hypothesis is negligible, we fail to reject the null hypothesis, we can proceed with our hypothesis testing

We are assuming that to have a considerable difference in the means of S1_Light and S2_Light the difference should at least be 50 lux

test <- pwrss.t.2means(mu1 = 0.2, 
                       sd1 = 1,
                       mu2 = 50,
                       sd2 = sd(pluck(sensor_data, "Light_diff")),
                       kappa = 1,
                       power = .80, alpha = 0.05, 
                       alternative = "not equal")
##  Difference between Two means 
##  (Independent Samples t Test) 
##  H0: mu1 = mu2 
##  HA: mu1 != mu2 
##  ------------------------------ 
##   Statistical power = 0.8 
##   n1 = 6 
##   n2 = 6 
##  ------------------------------ 
##  Alternative = "not equal" 
##  Degrees of freedom = 10 
##  Non-centrality parameter = -3.319 
##  Type I error rate = 0.05 
##  Type II error rate = 0.2
plot(test)

From the above power test we get that we need atleast 6 reading for both the sensors for our hypothesis test to maintain the strength defined.

Fisher’s Significance Testing

P Value

f_sampling <- function(x) dnorm(x, mean = 0, 
                                sd = sd(diffs_in_avgs))

ggplot() +
  stat_function(mapping = aes(fill = 'more extreme samples'),
                fun = f_sampling, 
                xlim = c(observed_diff, 25),
                geom = "area") +
  stat_function(mapping = aes(fill = 'more extreme samples'),
                fun = f_sampling, 
                xlim = c(-25, -observed_diff),
                geom = "area") +
  geom_function(xlim = c(-25, 25), 
                fun = f_sampling) +
  geom_vline(mapping = aes(xintercept = observed_diff,
                           color = paste("observed: ",
                                         round(observed_diff, 1)))) +
  labs(title = "Bootstrapped Sampling Distribution of Light Differences",
       x = "Difference in Light Calculated",
       y = "Probability Density",
       color = "",
       fill = "") +
  scale_x_continuous(breaks = seq(-25, 25, 5)) +
  scale_fill_manual(values = 'lightblue') +
  theme_minimal()

diffs_in_avgs_d <- diffs_in_avgs - mean(diffs_in_avgs)

# proportion of times the difference is more extreme
paste("p-value ", 
      sum(abs(observed_diff) < abs(diffs_in_avgs_d)) /
        length(diffs_in_avgs_d))
## [1] "p-value  0.948"

Conclusions:

With the above results we can definitely not reject the null hypothesis as P value is significantly larger than our significance value of 0.05. There is not much evidence to reject the null hypothesis. The Effect Size value also comes out as -0.009565797 which is less than our assumed minimum effect size value of 0.2, hence we can say that there is no significant difference between the means of samples of the sensor data.

Hypothesis II

Let’s define an Alternate Hypothesis first

Null Hypothesis: There is no significant relationship between the S1_Sound intensity readings and the room occupancy count.

Alternate Hypothesis: There is a significant relationship between the S1_Sound intensity reading and the room occupancy count.

This is a Two Tailed Test

Some important statistical assumptions to be made for Hypothesis Testing

  • Alpha Level or Significance Value:
    • Value Chosen: 0.05
    • Reason: Alpha levels are chosen based on the context and based on how low the level of Type-1 error should be. Since, this isn’t a very important or a critical issue I am going with the standard value of 0.05 which is a standard choice that aligns with common statistical practices.
  • Power Level:
    • Value Chosen: 0.08
    • Reason: This is the probability of correctly rejecting the Null Hypothesis. I chose 0.08 because it is a standard choice that aligns with common statistical practices.
  • Minimum Effect Size:
    • Value Chosen: 0.20
    • Reason: This is the the minimum value for effect size which should be considered while comparing two samples of the same population, smaller the effect size smaller the difference between the two sampling distributions. I am going with 0.20 as my minimum effect size as it is a common statistical practices.

Neyman-Pearson Testing

sensor_data_1 = filter(sensor_data, Room_Occupancy_Count > 0)
sensor_data_0 = filter(sensor_data, Room_Occupancy_Count <= 0)
observed_mean = abs(mean(sensor_data_0$S1_Sound) - mean(sensor_data_1$S1_Sound))
diff_in_avg <- function (x_data) {
  sound_samples <- mean(x_data$S2_Light)
  return(sound_samples)
}

diffs_in_avgs <- bootstrap(sensor_data, diff_in_avg, n_iter = 1000)
ggplot() +
  geom_function(xlim = c(-50, 50), 
                fun = function(x) dnorm(x, mean = 0, 
                                        sd = sd(diffs_in_avgs))) +
  geom_vline(mapping = aes(xintercept = observed_mean,
                           color = paste("observed: ",
                                         round(observed_mean)))) +
  labs(title = "Bootstrapped Sampling Distribution of mean Light Difference",
       x = "Observed Mean of S1_Sound",
       y = "Probability Density",
       color = "") +
  scale_x_continuous(breaks = seq(-50, 50, 10)) +
  theme_minimal()

cohen.d(d = filter(sensor_data, Room_Occupancy_Count > 0) |> pluck("S1_Sound"),
        f = filter(sensor_data, Room_Occupancy_Count <= 0) |> pluck("S1_Sound"))
## 
## Cohen's d
## 
## d estimate: 1.918948 (large)
## 95 percent confidence interval:
##    lower    upper 
## 1.862498 1.975399

The effect size is greater than 0.5 so the difference between the both hypothesis is high.

We are assuming that to have a considerable difference in the means of S1_Sound the value of ADC output should be 3 db

test <- pwrss.t.2means(mu1 = 0.2, 
                       sd1 = 1,
                       mu2 = 3,
                       sd2 = sd(pluck(sensor_data, "S1_Sound")),
                       kappa = 1,
                       power = .80, alpha = 0.05, 
                       alternative = "not equal")
##  Difference between Two means 
##  (Independent Samples t Test) 
##  H0: mu1 = mu2 
##  HA: mu1 != mu2 
##  ------------------------------ 
##   Statistical power = 0.8 
##   n1 = 3 
##   n2 = 3 
##  ------------------------------ 
##  Alternative = "not equal" 
##  Degrees of freedom = 4 
##  Non-centrality parameter = -4.623 
##  Type I error rate = 0.05 
##  Type II error rate = 0.2
plot(test)

Fisher’s Significance Testing

P Value

f_sampling <- function(x) dnorm(x, mean = 0, 
                                sd = sd(diffs_in_avgs))

ggplot() +
  stat_function(mapping = aes(fill = 'more extreme samples'),
                fun = f_sampling, 
                xlim = c(observed_diff, 40),
                geom = "area") +
  stat_function(mapping = aes(fill = 'more extreme samples'),
                fun = f_sampling, 
                xlim = c(-40, -observed_mean),
                geom = "area") +
  geom_function(xlim = c(-40, 40), 
                fun = f_sampling) +
  geom_vline(mapping = aes(xintercept = observed_mean,
                           color = paste("observed: ",
                                         round(observed_mean, 1)))) +
  labs(title = "Bootstrapped Sampling Distribution of Light Differences",
       x = "Difference in Sound Calculated",
       y = "Probability Density",
       color = "",
       fill = "") +
  scale_x_continuous(breaks = seq(-40, 40, 10)) +
  scale_fill_manual(values = 'lightblue') +
  theme_minimal()

lm_model <- lm(Room_Occupancy_Count ~ S1_Sound, data = sensor_data)

se_coef <- summary(lm_model)$coef["S1_Sound", "Std. Error"]

t_statistic <- coef(lm_model)["S1_Sound"] / se_coef

df <- nrow(sensor_data) - length(coef(lm_model))

p_value <- 2 * pt(abs(t_statistic), df = df, lower.tail = FALSE)

cat("t-statistic:", t_statistic, "\n")
## t-statistic: 70.49545
cat("p-value:", p_value, "\n")
## p-value: 0

Conclusions:

With the above results we can not reject the null hypothesis but we can say that there is strong evidence against it as P value is significantly smaller than our significance value of 0.05. The Effect Size value also comes out as 1.918948 which is very high than our assumed minimum effect size value of 0.2, hence we can say that there is significant difference between the means of samples of the sensor data.