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(boot)
library(broom)
library(lindia)
sensor_data <- read.csv("/Users/manojdannana/Desktop/Manoj/IUPUI/class work/intro to statistics/datasets/Occupancy_Estimation.csv")
Creating 3 more columns for our analysis like last week
sensor_data$Mean_Temp <- rowMeans(sensor_data[, c("S1_Temp", "S2_Temp", "S3_Temp", "S4_Temp")])
sensor_data$Mean_Sound <- rowMeans(sensor_data[, c("S1_Sound", "S2_Sound", "S3_Sound", "S4_Sound")])
sensor_data$Mean_Light <- rowMeans(sensor_data[, c("S1_Light", "S2_Light", "S3_Light", "S4_Light")])
head(sensor_data)
## 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 Mean_Temp Mean_Sound Mean_Light
## 1 0 1 24.9075 0.0975 62.00
## 2 0 1 24.9225 0.2750 61.75
## 3 0 1 24.9225 0.1700 62.00
## 4 0 1 24.9375 0.1750 62.00
## 5 0 1 24.9375 0.0900 62.25
## 6 0 1 24.9525 0.0800 62.25
model_1 <- lm(Room_Occupancy_Count ~ Mean_Temp + Mean_Sound
+ Mean_Light, sensor_data)
tidy(model_1, conf.int = TRUE)
## # A tibble: 4 × 7
## term estimate std.error statistic p.value conf.low conf.high
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) -10.5 0.377 -27.7 5.74e-163 -11.2 -9.72
## 2 Mean_Temp 0.411 0.0149 27.5 5.45e-161 0.382 0.440
## 3 Mean_Sound 0.593 0.0245 24.2 1.18e-125 0.545 0.641
## 4 Mean_Light 0.0128 0.000154 83.4 0 0.0125 0.0131
summary(model_1)
##
## Call:
## lm(formula = Room_Occupancy_Count ~ Mean_Temp + Mean_Sound +
## Mean_Light, data = sensor_data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.26300 -0.10614 -0.00447 0.09066 2.67191
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.046e+01 3.774e-01 -27.71 <2e-16 ***
## Mean_Temp 4.109e-01 1.492e-02 27.53 <2e-16 ***
## Mean_Sound 5.932e-01 2.453e-02 24.18 <2e-16 ***
## Mean_Light 1.282e-02 1.538e-04 83.41 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4335 on 10125 degrees of freedom
## Multiple R-squared: 0.7647, Adjusted R-squared: 0.7647
## F-statistic: 1.097e+04 on 3 and 10125 DF, p-value: < 2.2e-16
gg_resfitted(model_1) +
geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
we can already see that one of our assumptions is being violated, in that the variance in errors increases for higher room occupancy.
gg_resX(model_1, plot.all = FALSE)
## $Mean_Temp
##
## $Mean_Sound
##
## $Mean_Light
What we want is for a constant variance of residuals across values of X variables. This is a violation of the constant variance assumption for explanatory variables. Mean_Temp indicates higher residuals for intermediary values, Mean_Sound displays higher residuals for lower values.
gg_reshist(model_1)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
When we look at the histogram of residuals, we want to see a distribution that is roughly normal. We see that there is a slight concern, as the tails are not equally distributed. There is a “shorter” tail on the left, and a longer one on the right. This isn’t expected and this indicates that we might have to change the explanatory variables
gg_qqplot(model_1)
We expect to see a symmetrical curve along the line but this is not the case.
gg_cooksd(model_1, threshold = 'matlab')
From the above graph we see that there are a few observations that have a significant effect on our model. This is not a sign of a good model
Hence we can conclude that the model generated using the variables Mean_Temp, Mean_sound and Mean_Light as explanatory variables is not a good fit.