This repository has been archived by the owner on May 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotting_script.R
84 lines (75 loc) · 4.44 KB
/
plotting_script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
source('load_packages_files_and_functions.R')
#######################################
## choose model: Version 1 = 15, Version 2 = 16, Version 3 = 17
models <- 15:17
#-------edit---------#-#
which_model <- models[1]
#--------------------#-#
fit.file <- paste0('fitYear',which_model,'.Rdata')
fit <- readRDS(fit.file)
#######################################
x_axes <- c("strike_age","strike_male","roadtype","cas_severity","year","cas_male","cas_mode","strike_mode","cas_age")
#------EDIT------#--------------------------#
## Step 1: choose what to plot over
x_axis <- x_axes[9]
## Step 2: choose which categories to keep from each covariate
years <- 2015 # 2005:2015
severity <- 'Fatal' # c('Fatal','Serious','Slight')
road <- c("Motorway/A(M)", "A","B, C, Unclassified")
cas_gen <- c(0,1) ## 0 is female, 1 is male
str_gen <- c(0,1) ## 0 is female, 1 is male
casualty_mode <- 'car/taxi' # c('pedestrian',"cyclist","motorcycle","car/taxi","light goods","bus","heavy goods")
str_mode <- 'car/taxi' # c('pedestrian',"cyclist","motorcycle","car/taxi","light goods","bus","heavy goods")
cas_ages <- unique(test_data$cas_age)
str_ages <- unique(test_data$strike_age)
#----------------#--------------------------#
#------RUN------#
## Step 3: subset data
test_data0 <- subset(test_data,year%in%years&cas_male%in%cas_gen&cas_severity%in%severity&cas_mode%in%casualty_mode&
strike_mode%in%str_mode&strike_age%in%str_ages&cas_age%in%cas_ages&roadtype%in%road&strike_male%in%str_gen)
## Step 4: plot result
# x_axis is what to plot over;
# test_data0 is the subsetted dataset;
# fit is the glm object loaded from file;
# rate_per_casualty is a logical switch: set to 'TRUE' to divide by casualty exposure;
# rate_per_striker is a logical switch: set to 'TRUE' to divide by striker exposure.
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=T,rate_per_striker=F)
#---------------#
#######################################
#------EXAMPLES------#
## Ex 1
## plot rate of car/car fatality, by striker age per striker distance
x_axis <- 'strike_age'
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=F,rate_per_striker=T)
## Ex 2
## plot rate of ped/car fatality, by year (on road type 'B, C, etc')
x_axis <- 'year'
road <- 'B, C, Unclassified'
years <- 2005:2015
casualty_mode <- 'pedestrian'
test_data0 <- subset(test_data,year%in%years&cas_male%in%cas_gen&cas_severity%in%severity&cas_mode%in%casualty_mode&
strike_mode%in%str_mode&strike_age%in%str_ages&cas_age%in%cas_ages&roadtype%in%road&strike_male%in%str_gen)
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=F,rate_per_striker=F)
## same again, with rate per casualty distance
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=T,rate_per_striker=F)
## Ex 3
## compare with cyclist
casualty_mode <- 'cyclist'
test_data0 <- subset(test_data,year%in%years&cas_male%in%cas_gen&cas_severity%in%severity&cas_mode%in%casualty_mode&
strike_mode%in%str_mode&strike_age%in%str_ages&cas_age%in%cas_ages&roadtype%in%road&strike_male%in%str_gen)
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=F,rate_per_striker=F)
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=T,rate_per_striker=F)
## Ex 4
## plot rate of car/car fatality, by roadtype, per striker distance, comparing striker genders
x_axis <- 'roadtype'
road <- c("Motorway/A(M)", "A","B, C, Unclassified")
str_gen <- 0 ## 0 is female, 1 is male
casualty_mode <- 'car/taxi' # c('pedestrian',"cyclist","motorcycle","car/taxi","light goods","bus","heavy goods")
test_data0 <- subset(test_data,year%in%years&cas_male%in%cas_gen&cas_severity%in%severity&cas_mode%in%casualty_mode&
strike_mode%in%str_mode&strike_age%in%str_ages&cas_age%in%cas_ages&roadtype%in%road&strike_male%in%str_gen)
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=F,rate_per_striker=T)
str_gen <- 1 ## 0 is female, 1 is male
test_data0 <- subset(test_data,year%in%years&cas_male%in%cas_gen&cas_severity%in%severity&cas_mode%in%casualty_mode&
strike_mode%in%str_mode&strike_age%in%str_ages&cas_age%in%cas_ages&roadtype%in%road&strike_male%in%str_gen)
plot_observed_and_prediction(x_axis=x_axis,test_data0=test_data0,fit=fit,rate_per_casualty=F,rate_per_striker=T)
#--------------------#