Advertisement
Guest User

Untitled

a guest
Oct 27th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. # Load required libraries
  2. library(tidyverse)
  3.  
  4. # Load the data
  5. inbound <- read.csv("/path/to/inbound.csv", stringsAsFactors = FALSE)
  6. outbound <- read.csv("/path/to/outbound.csv", stringsAsFactors = FALSE)
  7.  
  8. # Compute speed differences
  9. compute_speed_difference <- function(data) {
  10. data <- data %>%
  11. arrange(X..Timestamp) %>%
  12. group_by(Name) %>%
  13. mutate(speed_difference = c(0, diff(SOG)))
  14.  
  15. return(data)
  16. }
  17.  
  18. inbound <- compute_speed_difference(inbound)
  19. outbound <- compute_speed_difference(outbound)
  20.  
  21. # Combine the datasets and differentiate direction
  22. inbound$Direction <- "Inbound"
  23. outbound$Direction <- "Outbound"
  24. combined_data <- rbind(inbound, outbound)
  25.  
  26. # Filter for the ship of interest
  27. combined_data <- combined_data %>% filter(Name == "NEWNEW POLAR BEAR")
  28.  
  29. # Categorize the data into the two bins
  30. #combined_data$New_Speed_Bins <- ifelse(abs(combined_data$speed_difference) <= 0.1, "Speed delta: -0.1 to 0.1", "> -0.1 to 0.1")
  31. combined_data$New_Speed_Bins <- ifelse(abs(combined_data$speed_difference) <= 0.2, "-Speed delta: 0.2 to 0.2", "> -0.2 to 0.2")
  32.  
  33. # Create a contingency table of counts using the new bins
  34. new_contingency_table <- table(combined_data$Direction, combined_data$New_Speed_Bins)
  35.  
  36. # Conduct the Chi-Squared Test for Independence
  37. new_chi_squared_test <- chisq.test(new_contingency_table)
  38.  
  39. # Print the new contingency table for inspection
  40. print(new_contingency_table)
  41.  
  42. # Print the Chi-Squared test results
  43. new_chi_squared_test
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement