R Worksop

setwd("C:/Users/mvx13/Desktop/R_Workshop")
dat= read.csv("Text Network.csv")
dim(dat)
## [1] 10  4
head(dat)
##   X ID      Type
## 1 1 57 Ambulance
## 2 2 58 Ambulance
## 3 3 59 Ambulance
## 4 4 60 Ambulance
## 5 5 61 Ambulance
## 6 6 62 Ambulance
##                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    World
## 1                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             the crash occurred on a rural twolane statemaintained roadway during the evening illumination conditions were twilight shortly after civil twilight without artificial lighting at the time of the crash the national weather service reported cloudy skies with moderate rain a temperature of 10 ºc 50 ºf 100 percent relative humidity and a 8 kmh 5 mph easterly breeze the police crash report par listed the environmental conditions as dusk rain and wet\n the northsouth roadway consisted of a single travel lane in each direction divided by a doublesolid yellow centerline and delineated by singlesolid white fog lines each lane measured 40 m 131 ft wide there were no shoulders in the vicinity of the crash the east roadside was populated by various trees and other vegetation the west roadside was the expansive front lawn of a private residence with a wooden fence that paralleled the roadway 55 m 180 ft west of the west road edge the roadway was asphalt surfaced speed in both directions was regulated by a posted limit of 89 kmh 55 mph continuous tactile rumble strips were cut into the roadway surface immediately inboard of the fog lines figure 2 shows a southfacing view of the roadway for the ambulances precrash travel trajectory while figure 3 shows the northbound trajectory of the ram a crash diagram showing a schematic of the crash sequence is included at the end of this report the private ambulance agency was a multitiered medical transport service not associated with any particular medical treatment center it was capable of providing all levels of ems care from basic life support to advanced critical care the agency performed public emergency response interfacility transfers private requests and specialty transports using a fleet primarily of type ii ambulances the ambulance agency employed career professionals who consisted of administrative staff support personnel dispatchers and emts of varying levels of care the agency maintained its equipment and operated in compliance with all ohio department of public safety regulations the ambulance agency required its employees who operated vehicles to complete emergency vehicle operations training the 28yearold emt driver had taken the training several times with his last certification in march 2018\n ems personnel were primarily scheduled on 24hour shift rotations the emt driver had begun his 24hour shift on the morning of the crash he was approximately halfway into his shift when the crash occurred and had not reported any issues concerning fatigue he stated to law enforcement during the interview that he had slept for 6 hours the night before the frontrow right paramedic was working a backhalf cover which entailed working the second half of a shift to cover for a different employee she had begun her onduty time less than 30 minutes before the crash occurred\n approximately a year prior to this crash the ambulance agency instituted a fatigue policy that allowed its employees to take an extended break for rest andor leave work early at their request after having several backtoback transports or even just one longduration transport management encouraged its employees to take advantage of this benefit and selfreported that they had seen a decrease in onduty fatigue
## 2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    the crash occurred in the interchange of a fourlane interstate highway during the afternoon hours reported weather conditions in the locale included partly cloudy conditions and a temperature of 30 ºc 86 ºf the trafficway traversed in a general northsouth direction with two bituminous through lanes in each direction separated by a wide grassy median and cable barrier figure 2 the lanes were 40 m 131 ft wide the northbound roadway curved to the left with a radius of 1142 m 37467 ft and was positively sloped at 3 percent the roadway was slightly superelevated but due to safety concerns from high traffic a measurement was not obtained it was also bordered by bituminous shoulders with rumble strips and metal guardrails roadway markings consisted of a solid white edge line a dashed white lane line and a solid yellow median line the posted speed limit was 113 kmh 70 mph the roadway surface was dry at the time of the crash a crash diagram is included at the end of this report figure 2 northfacing view of the roadway and pre crash approach of the jeep
## 3                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              the crash site was the intersection of an undivided northsouthbound roadway and an undivided eastwestbound roadway the northbound roadway was composed of two northbound travel 1 lanes a left turn lane and two southbound travel lanes figure 2 the concrete roadway was straight and level the westbound roadway was composed of two westbound travel lanes a left turn lane and a wide eastbound lane figure 3 the concrete roadway was straight and level there were tall willow bushes in the southeast corner of the intersection obscuring the view looking south the intersection was controlled by 3phase overhead traffic signals the speed limit was 80 kmh 50 mph for both roadways the weather at the nearest reporting station was figure 2 northbound approach the 12 °c 53 °f 74 percent humidity fair ambulance conditions and the winds were out of the west northwest at 11 kmh 7 mph crash diagrams are included at the end of this technical report
## 4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              this crash occurred during the afternoon on a fivelane undivided us highway the weather conditions were cloudy north winds at 32 kmh 20 mph a temperature of 17 °c 35 °f and dew point of 55 °c 22 °f according to local weather reports the roadway was straight and traversed in an eastwest direction there were two through lanes in each direction and a bi directional center left turn lane each lane was approximately 31 m 102 ft wide the roadway pavement markings consisted of solid white edge lines broken white lane lines solidbroken yellow bidirectional turn lane lines and white bidirectional left turn arrows the roadway surface was bituminous with a positive 3 negative grade in the eastbound direction a narrow paved and stone shoulder bordered the eastbound lane the south roadside consisted of a level grass area that transitioned to a wooded area with a negative slope and a drainage ditch a w beam guardrail system protected the eastbound traffic flow from the wooded area and the ditch area the posted speed limit was 80 kmh 50 mph the crash diagram is included at the end of this report
## 5                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              this crash occurred during the night in an interchange and the east side of a fourlane divided interstate highway the area of the crash was illuminated by artificial overhead lighting the weather conditions were cloudy with clear visibility and southsoutheast winds at 16 kmh 84 mph a temperature of 228 °c 73 °f and a dew point of 217 °c 71 °f according to local weather reports the roadway traversed a northsouth direction and had two concrete northbound through lanes bordered by concrete shoulders the travel lanes curved to the left with a measured radius of 300 m 9842 ft and had a positive grade of 25 percent the right and left northbound through lanes were 37 m 121 ft and 33 108 ft wide respectively the right and median shoulders were 28 m 92 ft and 20 m 66 ft wide respectively a smart cushion brand speeddependent impact attenuator hill  smith inc columbus oh that protected errant vehicles from the end of a concrete jersey barrier was located on the east right side of the roadway adjacent to the shoulder a grass embankment that transitioned to a drainage ditch was located east of the roadside barrier ditch a woven wire rightofway fence was located figure 2 northbound approach of the beyond the drainage ditch on the east side of the ambulance to the roadway departure roadway the speed limit was 97 kmh 60 mph the crash diagram is included at the end of this report
## 6 the crash occurred on a fourlane roadway overnight environmental conditions reported by the national weather service in the vicinity at the time of the crash included clear skies with a temperature of 22 ºc 71 ºf 100percent relative humidity and calm winds a nikon nivo 5+m total station mapping system and photographs were used to document the crash site during the sci inspection a crash diagram is included at the end of this report\n the multilane roadway was oriented in the northsouth direction with a left curve for southbound traffic the radius of the curve measured 742 m 2435 ft respective travel directions were separated by a double yellow centerline the two southbound lanes measured 35 m 1\n figure 2 southfacing view depicting the roadway configuration and the ambulances precrash trajectory\n 15 ft wide and were separated by broken white lines with a solid white line delineating the travel lanes from the 13 m 42 ft wide shoulder a continuous fullwidth rumble strip was cut into the surface of the shoulder figure 2 depicts a southbound view of the roadway for the ambulances precrash travel trajectory the speed of traffic on the multilane roadway was controlled by a posted limit of the 72 kmh 45 mph\n a local road intersected the apex of the curve from the west the primary roadway progressed along a 16percent downgrade in the southbound direction beginning in the area of the intersection the west roadside consisted of an expansive grass area that was populated with large diameter trees dispersed approximately 30 m 100 ft apart a tree in the roadside located 105 m 344 ft from the west edge line of the multilane roadway and 282 m 925 ft south of the intersecting local road was the focal point of impact the tree was fractured and uprooted by the associated impact forces all evidence of the tree was removed at the time of the sci crash site inspection\n ambulance agency crew and transport description\n the ambulance agency declined to participate in the sci investigation and refused to provide any information to the sci investigator concerning its operational procedures certification requirements training programs or employee schedules a statement released by the agency to the media reported that all drivers were required to complete eight hours of emergency vehicle operations training be supervised in the field for 48 hours and be recertified every year a review of the agencys website revealed that it is a multitiered medical transport service not associated with any particular medical treatment center and was capable of providing ems care at both the basic life support bls and advanced levels als the agency performs emergency response mutual aid interfacility transfers private requests and specialty transports over multiple response areas in georgia and tennessee using a variety of fleet ambulances and emergency response vehicles it employs an allcareer staff of management support personnel drivers dispatchers and emts of varying levels of care\n the sci investigators attempts to interview the involved ems crewmembers were refused by the ems agency citing pending criminal and civil legal concerns specifics concerning the driver and the events leading up to the crash were documented by the investigating law enforcement agency and became public news in the weeks following the crash through statements released to the media by the drivers attorney from these multiple sources the sci investigator determined the following summarized circumstances concerning the driver and the incident transport\n the 21yearoldfemale driver of the ambulance reported to her assigned work shift at 1900 hours during the evening prior to this overnight crash she was scheduled to a work a 12hour shift and had been onduty for 9 hours when the crash occurred the driver was credentialed as an advanced emergency medical technician aemt of note she also held secondary employment it is unknown if she had completed a work shift for her secondary employer prior to the start of this assigned shift the driver discovered at some point in the days preceding the crash that she was 5 weeks pregnant after experiencing illness and fatigue as a result she had stopped consuming the caffeinated beverages that she ordinarily had relied upon to help her maintain her alertness while onduty she also had a history of long qt syndrome lqts a condition of repolarization of the heart that has been known to cause sudden and uncontrollable arrhythmias potentially resulting in fainting seizures or sudden death1 the driver had experienced at least one episode related to her lqts condition during the prior year specific timeplace unknown\n no specifics were available concerning the 29yearold male emt his work schedule or his levels of trainingcertification\n the involved ems crewmembers had been dispatched to respond to an area nursing facility to transport the 55yearold male patient to a hospital the patient was a morbidly obese individual with history of heart disease and deep vein thrombosis who was to be transported from the nursing facility to a regional medical facility for care the transport was executed in a non emergency mode without the use of emergency warning lights or siren the hospital was 668 km 415 mi south of the nursing facility this distance was estimated to take approximately 54 minutes of total travel time the crash occurred after the ambulance had traveled without incident for 63 km 39 mi approximately 45 minutes into the trip no specifics concerning the onduty shift activities of the ems crew prior to the incident transport were available

Loading Packages

library(tm)
## Warning: package 'tm' was built under R version 4.3.1
## Loading required package: NLP
library(tidytext)
## Warning: package 'tidytext' was built under R version 4.3.1
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.1
## Warning: package 'ggplot2' was built under R version 4.3.1
## Warning: package 'tibble' was built under R version 4.3.1
## Warning: package 'tidyr' was built under R version 4.3.1
## Warning: package 'readr' was built under R version 4.3.1
## Warning: package 'purrr' was built under R version 4.3.1
## Warning: package 'dplyr' was built under R version 4.3.1
## Warning: package 'stringr' was built under R version 4.3.1
## Warning: package 'forcats' was built under R version 4.3.1
## Warning: package 'lubridate' was built under R version 4.3.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ ggplot2::annotate() masks NLP::annotate()
## ✖ 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(quanteda)
## Warning: package 'quanteda' was built under R version 4.3.1
## Warning in .recacheSubclasses(def@className, def, env): undefined subclass
## "pcorMatrix" of class "replValueSp"; definition not updated
## Warning in .recacheSubclasses(def@className, def, env): undefined subclass
## "pcorMatrix" of class "xMatrix"; definition not updated
## Warning in .recacheSubclasses(def@className, def, env): undefined subclass
## "pcorMatrix" of class "mMatrix"; definition not updated
## Package version: 3.3.1
## Unicode version: 13.0
## ICU version: 69.1
## Parallel computing: 8 of 8 threads used.
## See https://quanteda.io for tutorials and examples.
## 
## Attaching package: 'quanteda'
## 
## The following object is masked from 'package:tm':
## 
##     stopwords
## 
## The following objects are masked from 'package:NLP':
## 
##     meta, meta<-
library(dplyr)
library(quanteda)
library(dplyr)
clean_texts <- function(x) {
  x %>%
    str_remove_all(" ?(f|ht)(tp)(s?)(://)(.*)[.|/](.*)") %>%
    str_replace_all("&amp;", "and") %>%
    str_remove_all("[[:punct:]]") %>%
    str_remove_all("^RT:? ") %>%
    str_remove_all("@[[:alnum:]]+") %>%
    str_remove_all("#[[:alnum:]]+") %>%
    str_remove_all("pictwittercom") %>%
    str_replace_all("///n", " ") %>%
    str_to_lower() %>%
    str_trim("both")
}

dat$World= dat$World %>% clean_texts 


corp1 <- corpus(dat$World)
all_stops <- c("crash","crashes","ambulance","driver", "vehicle" , "m", "f", "ft", "cot",
               "vehicles", "cars", "na", "mph", "kmh", "winsmash", "solara", "yaris","c",
               "ae", "edr", "v", "b", "d", "e", "black", "white", "blue", "red", "g", "h", "k",
               "n", "o", "p", "q", "s", "t", "u", "w", "x", "y", "z",
               "sec","secs","cm","x","crs","sci","westbound","eastbound","southbound","northbound", stopwords("en"))

dfm1 <- corp1 |>
  tokens(remove_punct = TRUE, remove_numbers = TRUE) %>%
  tokens_remove(pattern = all_stops) %>%
  dfm()


tag_dfm <- dfm_select(dfm1)
toptag <- names(topfeatures(tag_dfm, 30))
tag_fcm <- fcm(tag_dfm)
topgat_fcm <- fcm_select(tag_fcm, pattern = toptag)

library(quanteda.textplots)
## Warning: package 'quanteda.textplots' was built under R version 4.3.1
textplot_network(topgat_fcm, min_freq = 0.1, edge_alpha = 0.4,
                 edge_color = "#798E87", edge_size = 2,
                 vertex_labelsize = 8)

```