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
df <- read.csv("/Users/manojdannana/Desktop/Manoj/IUPUI/class work/intro to statistics/datasets/Occupancy_Estimation.csv")
df$Datetime <- as.POSIXct(paste(df$Date, df$Time), format="%d/%m/%y %H:%M:%S")

Grouping Categorical Columns and Aggragating ‘Room_Occupancy_Count’

grouped_df1 <- aggregate(Room_Occupancy_Count ~ S1_Light, data=df, FUN=mean)
print(grouped_df1)
##    S1_Light Room_Occupancy_Count
## 1         0          0.001026518
## 2         1          0.000000000
## 3         2          0.142857143
## 4         3          0.600000000
## 5         4          0.547826087
## 6         5          0.334532374
## 7         6          0.108108108
## 8         7          0.331034483
## 9         8          0.344827586
## 10        9          0.663716814
## 11       10          0.591743119
## 12       11          0.000000000
## 13       12          0.000000000
## 14       13          0.000000000
## 15       14          0.000000000
## 16       15          0.000000000
## 17       16          0.000000000
## 18       17          0.000000000
## 19       18          0.000000000
## 20       19          0.000000000
## 21       20          0.000000000
## 22       91          0.000000000
## 23       93          1.000000000
## 24      101          1.000000000
## 25      105          1.000000000
## 26      107          1.000000000
## 27      108          1.000000000
## 28      111          1.000000000
## 29      112          1.000000000
## 30      113          2.000000000
## 31      114          1.982456140
## 32      115          1.723076923
## 33      116          1.602941176
## 34      117          1.724489796
## 35      118          1.559523810
## 36      119          1.275590551
## 37      120          1.000000000
## 38      121          1.000000000
## 39      122          1.000000000
## 40      123          1.021739130
## 41      124          1.031250000
## 42      125          1.000000000
## 43      133          3.000000000
## 44      140          3.000000000
## 45      141          2.500000000
## 46      142          2.416666667
## 47      143          2.400000000
## 48      144          3.000000000
## 49      145          2.125000000
## 50      146          2.035714286
## 51      147          2.238095238
## 52      148          2.907407407
## 53      149          3.000000000
## 54      150          2.984210526
## 55      151          2.857142857
## 56      153          2.000000000
## 57      154          2.000000000
## 58      155          1.961538462
## 59      156          2.030303030
## 60      157          2.000000000
## 61      158          2.666666667
## 62      159          2.333333333
## 63      160          2.139534884
## 64      161          2.060606061
## 65      162          2.344827586
## 66      163          2.862068966
## 67      164          2.933333333
## 68      165          2.733333333
grouped_df2 <- aggregate(Room_Occupancy_Count ~ S6_PIR, data=df, FUN=mean)
print(grouped_df2)
##   S6_PIR Room_Occupancy_Count
## 1      0            0.2204861
## 2      1            2.1960570
grouped_df3 <- aggregate(Room_Occupancy_Count ~ S7_PIR, data=df, FUN=mean)
print(grouped_df3)
##   S7_PIR Room_Occupancy_Count
## 1      0            0.2159176
## 2      1            2.5111663

Identify the lowest probability group in each grouping

min_prob_group1 <- grouped_df1[which.min(grouped_df1$Room_Occupancy_Count), 'S1_Light']
print(min_prob_group1)
## [1] 1
min_prob_group2 <- grouped_df2[which.min(grouped_df2$Room_Occupancy_Count), 'S6_PIR']
print(min_prob_group2)
## [1] 0
min_prob_group3 <- grouped_df3[which.min(grouped_df3$Room_Occupancy_Count), 'S7_PIR']
print(min_prob_group3)
## [1] 0

Assigning Special Tags to Minimum Probability Groups and Appending to main DataFrame

df$Special_Tag1 <- ifelse(df$S1_Light == min_prob_group1, 'Lowest_Prob_Group1', 'Other')
df$Special_Tag2 <- ifelse(df$S6_PIR == min_prob_group2, 'Lowest_Prob_Group2', 'Other')
df$Special_Tag3 <- ifelse(df$S7_PIR == min_prob_group3, 'Lowest_Prob_Group3', 'Other')
head(df)
##       Date     Time S1_Temp S2_Temp S3_Temp S4_Temp S1_Light S2_Light S3_Light
## 1 22/12/17 10:49:41   24.94   24.75   24.56   25.38      121       34       53
## 2 22/12/17 10:50:12   24.94   24.75   24.56   25.44      121       33       53
## 3 22/12/17 10:50:42   25.00   24.75   24.50   25.44      121       34       53
## 4 22/12/17 10:51:13   25.00   24.75   24.56   25.44      121       34       53
## 5 22/12/17 10:51:44   25.00   24.75   24.56   25.44      121       34       54
## 6 22/12/17 10:52:14   25.00   24.81   24.56   25.44      121       34       54
##   S4_Light S1_Sound S2_Sound S3_Sound S4_Sound S5_CO2 S5_CO2_Slope S6_PIR
## 1       40     0.08     0.19     0.06     0.06    390    0.7692308      0
## 2       40     0.93     0.05     0.06     0.06    390    0.6461538      0
## 3       40     0.43     0.11     0.08     0.06    390    0.5192308      0
## 4       40     0.41     0.10     0.10     0.09    390    0.3884615      0
## 5       40     0.18     0.06     0.06     0.06    390    0.2538462      0
## 6       40     0.13     0.06     0.06     0.07    390    0.1653846      0
##   S7_PIR Room_Occupancy_Count            Datetime Special_Tag1
## 1      0                    1 2017-12-22 10:49:41        Other
## 2      0                    1 2017-12-22 10:50:12        Other
## 3      0                    1 2017-12-22 10:50:42        Other
## 4      0                    1 2017-12-22 10:51:13        Other
## 5      0                    1 2017-12-22 10:51:44        Other
## 6      0                    1 2017-12-22 10:52:14        Other
##         Special_Tag2       Special_Tag3
## 1 Lowest_Prob_Group2 Lowest_Prob_Group3
## 2 Lowest_Prob_Group2 Lowest_Prob_Group3
## 3 Lowest_Prob_Group2 Lowest_Prob_Group3
## 4 Lowest_Prob_Group2 Lowest_Prob_Group3
## 5 Lowest_Prob_Group2 Lowest_Prob_Group3
## 6 Lowest_Prob_Group2 Lowest_Prob_Group3

Visualizations for all groups

library(ggplot2)
ggplot(grouped_df1, aes(x=S1_Light, y=Room_Occupancy_Count)) +
  geom_bar(stat='identity') +
  labs(title='Average Room Occupancy Count by S1_Light')

ggplot(grouped_df2, aes(x=S6_PIR, y=Room_Occupancy_Count)) +
  geom_bar(stat='identity') +
  labs(title='Average Room Occupancy Count by S6_PIR')

ggplot(grouped_df3, aes(x=S7_PIR, y=Room_Occupancy_Count)) +
  geom_bar(stat='identity') +
  labs(title='Average Room Occupancy Count by S7_PIR')

Hypothesis based on Lowest Probability Groups

Finding a combination for S1_Light and S6_PIR that doesn’t exist

all_combinations <- expand.grid(unique(df$S1_Light), unique(df$S6_PIR))
all_combinations_string <- paste(as.character(all_combinations$Var1), as.character(all_combinations$Var2), sep="_")
existing_combinations_string <- paste(df$S1_Light, df$S6_PIR, sep="_")
missing_combinations <- setdiff(all_combinations_string, existing_combinations_string)

print(missing_combinations)
##  [1] "101_0" "153_0" "107_0" "140_0" "93_0"  "108_0" "154_0" "91_0"  "105_0"
## [10] "133_0" "144_0" "20_1"  "17_1"  "15_1"  "112_1" "1_1"   "125_1" "111_1"

Reasons for missing combinations

Most Frequent and In-Frequent Combinations

combination_freq <- table(paste(df$S1_Light, df$S6_PIR))

most_common_combination <- names(combination_freq)[which.max(combination_freq)]
least_common_combination <- names(combination_freq)[which.min(combination_freq)]

print(most_common_combination)
## [1] "0 0"
print(least_common_combination)
## [1] "101 1"

Observations

Visualization

subset_df <- df[df$S1_Light == 101 & df$S6_PIR == 1, ]

# Create a bar plot
ggplot(subset_df, aes(x = factor(Room_Occupancy_Count), fill = factor(Room_Occupancy_Count))) +
  geom_bar() +
  labs(title = 'Room Occupancy Count for Least Common Combination', x = 'Room Occupancy Count', y = 'Frequency')

All the insights and observations have been written below each cell