library(readr)
squirrel_data <- read_csv("squirrel_data.csv")
## Rows: 15 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Location
## dbl (3): Squirrel ID, Reaction Time (s), Distance Ran to Tree (m)
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
colnames(squirrel_data) <- c("SquirrelID", "Location", "ReactionTime", "DistanceToTree")
summary(squirrel_data)
##    SquirrelID     Location          ReactionTime    DistanceToTree
##  Min.   : 1.0   Length:15          Min.   : 0.000   Min.   : 0.0  
##  1st Qu.: 4.5   Class :character   1st Qu.: 2.070   1st Qu.: 3.0  
##  Median : 8.0   Mode  :character   Median : 3.690   Median : 5.0  
##  Mean   : 8.0                      Mean   : 5.591   Mean   : 5.7  
##  3rd Qu.:11.5                      3rd Qu.: 5.850   3rd Qu.: 8.5  
##  Max.   :15.0                      Max.   :27.200   Max.   :14.0
library(ggplot2)

ggplot(squirrel_data, aes(x = DistanceToTree, y = ReactionTime)) +
  geom_point(size = 3, alpha = 0.8, color = "darkblue") +  # Larger points with consistent color
  labs(
    title = "Squirrel Reaction Time vs. Distance to Tree",
    x = "Distance to Tree (m)",
    y = "Reaction Time (s)"
  ) +
  theme_minimal(base_size = 15) +  # Use a larger base font size
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5),  # Center and bold the title
    plot.subtitle = element_text(hjust = 0.5),  # Center the subtitle
    axis.title = element_text(face = "bold")  # Bold axis titles
  )

model <- lm(ReactionTime ~ DistanceToTree, data = squirrel_data)
summary(model)
## 
## Call:
## lm(formula = ReactionTime ~ DistanceToTree, data = squirrel_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.2433 -3.9627 -2.4933  0.4751 21.6409 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)  
## (Intercept)      6.1906     3.2020   1.933   0.0753 .
## DistanceToTree  -0.1053     0.4613  -0.228   0.8231  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.077 on 13 degrees of freedom
## Multiple R-squared:  0.003989,   Adjusted R-squared:  -0.07263 
## F-statistic: 0.05206 on 1 and 13 DF,  p-value: 0.8231
library(ggplot2)

ggplot(squirrel_data, aes(x = DistanceToTree, y = ReactionTime)) +
  geom_point(size = 2.5, alpha = 0.8, color = "#0072B2") +  # Slightly smaller points
  geom_smooth(method = "lm", color = "red", linetype = "dashed", se = TRUE) +  # Regression line with confidence interval
  labs(
    title = "Regression Analysis: Reaction Time vs. Distance to Tree",
    subtitle = "Linear relationship between proximity to trees and reaction behavior",
    x = "Distance to Tree (m)",
    y = "Reaction Time (s)"
  ) +
  theme_minimal(base_size = 12) +  # Reduced base font size for text
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5, size = 14),  # Adjusted title size
    plot.subtitle = element_text(hjust = 0.5, size = 10),  # Adjusted subtitle size
    axis.title = element_text(face = "bold", size = 12),  # Reduced axis title size
    axis.text = element_text(size = 10)  # Reduced axis text size
  )
## `geom_smooth()` using formula = 'y ~ x'

library(ggplot2)

ggplot(squirrel_data, aes(x = Location, y = ReactionTime)) +
  geom_boxplot(fill = "#56B4E9", color = "black", alpha = 0.8, outlier.color = "red", outlier.shape = 16) +  # Custom colors and outliers
  labs(
    title = "Reaction Time by Location",
    subtitle = "Comparing reaction times across different locations",
    x = "Location",
    y = "Reaction Time (s)"
  ) +
  theme_minimal(base_size = 14) +  # Adjusted base font size
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5, size = 16),  # Bold, centered title
    plot.subtitle = element_text(hjust = 0.5, size = 12),  # Centered subtitle
    axis.title = element_text(face = "bold"),  # Bold axis titles
    axis.text.x = element_text(angle = 45, hjust = 1, size = 12),  # Rotated x-axis labels
    axis.text.y = element_text(size = 12)  # Adjusted y-axis text size
  )

# Subset data for squirrels at the Colonnade
colonnade_data <- subset(squirrel_data, Location == "Colonnade")

# Fit a linear regression model
colonnade_model <- lm(ReactionTime ~ DistanceToTree, data = colonnade_data)

# Summary of the regression model
summary(colonnade_model)
## 
## Call:
## lm(formula = ReactionTime ~ DistanceToTree, data = colonnade_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.1855 -2.8984 -0.5355  1.1119  6.4319 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)  
## (Intercept)      5.6636     2.0567   2.754   0.0283 *
## DistanceToTree  -0.1652     0.3404  -0.485   0.6423  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.053 on 7 degrees of freedom
## Multiple R-squared:  0.03254,    Adjusted R-squared:  -0.1057 
## F-statistic: 0.2355 on 1 and 7 DF,  p-value: 0.6423
# Visualize the regression for Colonnade squirrels
library(ggplot2)

ggplot(colonnade_data, aes(x = DistanceToTree, y = ReactionTime)) +
  geom_point(size = 2.5, alpha = 0.8, color = "#0072B2") +  # Slightly smaller points
  geom_smooth(method = "lm", color = "red", linetype = "dashed", se = TRUE) +  # Dashed regression line with confidence interval
  labs(
    title = "Colonnade Squirrels: Reaction Time vs. Distance to Tree",
    subtitle = "Linear relationship between distance to tree and reaction time",
    x = "Distance to Tree (m)",
    y = "Reaction Time (s)"
  ) +
  theme_minimal(base_size = 10) +  # Reduced base font size
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5, size = 12),  # Smaller title
    plot.subtitle = element_text(hjust = 0.5, size = 10),  # Smaller subtitle
    axis.title = element_text(face = "bold", size = 10),  # Smaller axis titles
    axis.text = element_text(size = 8),  # Smaller axis text
    panel.grid.minor = element_blank()  # Remove minor grid lines for a cleaner look
  )
## `geom_smooth()` using formula = 'y ~ x'

interaction_model <- lm(ReactionTime ~ DistanceToTree * Location, data = squirrel_data)
summary(interaction_model)
## 
## Call:
## lm(formula = ReactionTime ~ DistanceToTree * Location, data = squirrel_data)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -4.186 -2.324  0.000  1.156  6.432 
## 
## Coefficients:
##                                          Estimate Std. Error t value Pr(>|t|)
## (Intercept)                                5.6636     1.8953   2.988  0.01524
## DistanceToTree                            -0.1652     0.3137  -0.527  0.61123
## LocationDhall Bridge                     -16.9536     6.2016  -2.734  0.02308
## LocationOutdoor Classroom                 -6.9626     7.8671  -0.885  0.39917
## DistanceToTree:LocationDhall Bridge        6.5802     1.3571   4.849  0.00091
## DistanceToTree:LocationOutdoor Classroom   0.6067     0.8699   0.697  0.50312
##                                             
## (Intercept)                              *  
## DistanceToTree                              
## LocationDhall Bridge                     *  
## LocationOutdoor Classroom                   
## DistanceToTree:LocationDhall Bridge      ***
## DistanceToTree:LocationOutdoor Classroom    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.735 on 9 degrees of freedom
## Multiple R-squared:  0.808,  Adjusted R-squared:  0.7013 
## F-statistic: 7.575 on 5 and 9 DF,  p-value: 0.00477
library(ggplot2)

ggplot(squirrel_data, aes(x = DistanceToTree, y = ReactionTime, color = Location)) +
  geom_point(size = 3, alpha = 0.8) +  # Larger, semi-transparent points
  geom_smooth(method = "lm", se = FALSE, linetype = "dashed", size = 1) +  # Dashed regression lines with consistent size
  labs(
    title = "Reaction Time by Distance and Location",
    subtitle = "Comparing linear trends across locations",
    x = "Distance to Tree (m)",
    y = "Reaction Time (s)",
    color = "Location"  # Legend title
  ) +
  theme_minimal(base_size = 12) +  # Adjusted base font size
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5, size = 14),  # Center and bold the title
    plot.subtitle = element_text(hjust = 0.5, size = 10),  # Center subtitle
    axis.title = element_text(face = "bold", size = 12),  # Bold axis titles
    axis.text = element_text(size = 10),  # Adjusted axis text size
    legend.position = "right",  # Move legend to the right for better space
    legend.title = element_text(face = "bold"),  # Bold legend title
    panel.grid.minor = element_blank()  # Remove minor grid lines for a cleaner look
  ) +
  scale_color_brewer(palette = "Set2")  # Apply a visually appealing color palette
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'

library(ggplot2)

ggplot(squirrel_data, aes(x = DistanceToTree, y = ReactionTime, color = Location)) +
  geom_point(size = 3, alpha = 0.8) +  # Larger points with semi-transparency
  geom_smooth(method = "lm", se = FALSE, linetype = "dashed", size = 1) +  # Dashed regression lines
  labs(
    title = "Reaction Time vs. Distance to Tree by Location",
    subtitle = "Linear trends for each location with shared axes",
    x = "Distance to Tree (m)",
    y = "Reaction Time (s)",
    color = "Location"  # Legend title
  ) +
  theme_minimal(base_size = 12) +  # Moderate base font size for readability
  theme(
    plot.title = element_text(face = "bold", hjust = 0.5, size = 14),  # Bold and centered title
    plot.subtitle = element_text(hjust = 0.5, size = 10),  # Centered subtitle
    axis.title = element_text(face = "bold", size = 12),  # Bold axis titles
    axis.text = element_text(size = 10),  # Adjusted axis text size
    legend.position = "right",  # Legend positioned on the right
    strip.text = element_text(face = "bold", size = 12),  # Bold facet labels
    panel.grid.minor = element_blank()  # Remove minor grid lines
  ) +
  facet_grid(. ~ Location)  # Facet by location with shared axes
## `geom_smooth()` using formula = 'y ~ x'