-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsolidate_data.R
162 lines (139 loc) · 7.5 KB
/
consolidate_data.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
if(!exists("is_run_parent")){
rm(list=ls())
# setwd("C:/Users/CATHY/OneDrive/Documents/2016-2017 Junior/15 Mines Research/violation-data-analysis")
setwd("~/Git/violation-data-analysis")
}
# Upper Big Branch: 4608436
# Inputs: raw accidents data and final data with only active mine-quarters
# Output: days lost, days restrict, deaths, and violations with only active mine-quarters
# setup
require(dplyr)
library(RcppRoll)
source("./Houston/src/prepare_violation.R")
source("./Houston/src/roll_over.R")
# load Accidents, Accidents.definition, AssessedViolation, AssedViolation.definition, Mines, Mines.definition, roll_over
if(!exists("Accidents") |
!exists("AssessedViolations") |
!exists("Mines")) {
load("./Houston/data/Accidents.RData")
load("./Houston/data/AssessedViolations.RData")
load("./Houston/data/Mines.RData")
print("raw RData loaded")
}
# constant parameters
longest_period <- 3
actual_start_year <- 2003
end_year <- 2015
# generate accident_roll_over and death_roll_over
actual_accidents <- Accidents %>% filter(is.element(DEGREE_INJURY, c("DAYS AWAY FROM WORK ONLY",
"DYS AWY FRM WRK & RESTRCTD ACT",
"DAYS RESTRICTED ACTIVITY ONLY",
"FATALITY",
"PERM TOT OR PERM PRTL DISABLTY",
"NO VALUE FOUND")))
# creates 2 new columns for death and perm disability. All NA in days lost and days restrict is changed to 0
actual_accidents["DEATH"] <- 0
actual_accidents["PERM_DIS"] <- 0
actual_accidents[is.na(actual_accidents$DAYS_LOST), "DAYS_LOST"] <- 0
actual_accidents[is.na(actual_accidents$DAYS_RESTRICT), "DAYS_RESTRICT)"] <- 0
actual_accidents[which(actual_accidents$DEGREE_INJURY == "FATALITY"), "DEATH"] <- 1
actual_accidents[which(actual_accidents$DEGREE_INJURY == "PERM TOT OR PERM PRTL DISABLTY"), "PERM_DIS"] <- 1
#edited to use date
actual_accidents$ai_dt_actual_date <- as.Date(actual_accidents$ACCIDENT_DT, format = "%m/%d/%Y")
###############################DAYS_LOST##################################################
quarter_level_num_days_lost <- actual_accidents %>% group_by(MINE_ID, CAL_QTR, CAL_YR) %>%
summarize(base = sum(DAYS_LOST, na.rm = T))
days_lost_rollover <- roll_over(actual_accidents,
quarter_level_num_days_lost,
longest_period,
actual_start_year,
end_year)
colnames(days_lost_rollover) <- c(
"MINE_ID", "QUARTER", "YEAR",
"NUM_DAYS_LOST", "LAST_QUARTER_DAYS_LOST", "LAST_YEAR_DAYS_LOST", "LAST_THREE_YEARS_DAYS_LOST"
)
rm(quarter_level_num_days_lost)
###########################DAYS_RESTRICT####################################################
quarter_level_num_days_restrict <- actual_accidents %>% group_by(MINE_ID, CAL_QTR, CAL_YR) %>%
summarize(base = sum(DAYS_RESTRICT, na.rm = T))
days_restrict_rollover <- roll_over(actual_accidents,
quarter_level_num_days_restrict,
longest_period,
actual_start_year,
end_year)
colnames(days_restrict_rollover) <- c(
"MINE_ID", "QUARTER", "YEAR",
"NUM_DAYS_RESTRICT", "LAST_QUARTER_DAYS_RESTRICT", "LAST_YEAR_DAYS_RESTRICT", "LAST_THREE_YEARS_DAYS_RESTRICT"
)
rm(quarter_level_num_days_restrict)
###########################DEATH##########################################################
quarter_level_num_deaths <- actual_accidents %>% group_by(MINE_ID, CAL_QTR, CAL_YR) %>%
summarize(base = sum(DEATH, na.rm = T))
death_rollover <- roll_over(actual_accidents,
quarter_level_num_deaths,
longest_period,
actual_start_year,
end_year)
colnames(death_rollover) <- c(
"MINE_ID", "QUARTER", "YEAR",
"NUM_DEATH", "LAST_QUARTER_DEATH", "LAST_YEAR_DEATH", "LAST_THREE_YEARS_DEATH"
)
rm(quarter_level_num_deaths)
################################PERM_DIS###################################################
quarter_level_num_perm_dis <- actual_accidents %>% group_by(MINE_ID, CAL_QTR, CAL_YR) %>%
summarize(base = sum(PERM_DIS, na.rm = T))
dis_rollover <- roll_over(actual_accidents,
quarter_level_num_perm_dis,
longest_period,
actual_start_year,
end_year)
colnames(dis_rollover) <- c(
"MINE_ID", "QUARTER", "YEAR",
"NUM_DIS", "LAST_QUARTER_DIS", "LAST_YEAR_DIS", "LAST_THREE_YEARS_DIS"
)
rm(quarter_level_num_perm_dis)
######################################VIOLATION###########################################
violation_altered <- prepare_violation(AssessedViolations)
quarter_level_viol_quantity <- violation_altered %>% group_by(MINE_ID, CAL_QTR, CAL_YR) %>%
summarize(base = sum(VIOLATION, na.rm = T))
violation_rollover <- roll_over(violation_altered,
quarter_level_viol_quantity,
longest_period,
actual_start_year,
end_year)
colnames(violation_rollover) <- c(
"MINE_ID", "QUARTER", "YEAR",
"VIOLATION_QUANTITY", "LAST_QUARTER_VIOLATION", "LAST_YEAR_VIOLATION", "LAST_THREE_YEARS_VIOLATION"
)
rm(quarter_level_viol_quantity )
################################PENALTY####################################################
quarter_level_viol_amount <- violation_altered %>% group_by(MINE_ID, CAL_QTR, CAL_YR) %>%
summarize(base = sum(PROPOSED_PENALTY_AMT, na.rm = T))
violation_penalty_rollover <- roll_over(violation_altered,
quarter_level_viol_amount,
longest_period,
actual_start_year,
end_year)
colnames(violation_penalty_rollover) <- c(
"MINE_ID", "QUARTER","YEAR",
"PROPOSED_PENALTY", "LAST_QUARTER_PENALTY", "LAST_YEAR_PENALTY", "LAST_THREE_YEARS_PENALTY"
)
rm(quarter_level_viol_amount)
# join accidents and violation
temp <- merge(days_lost_rollover, days_restrict_rollover, by = c("MINE_ID", "QUARTER","YEAR"),all=TRUE)
temp <- merge(temp, death_rollover, by = c("MINE_ID", "QUARTER","YEAR"),all=TRUE)
temp <- merge(temp, dis_rollover, by = c("MINE_ID", "QUARTER","YEAR"),all=TRUE)
temp <- merge(temp, violation_rollover, by = c("MINE_ID", "QUARTER","YEAR"),all.x=TRUE)
temp <- merge(temp, violation_penalty_rollover, by = c("MINE_ID", "QUARTER","YEAR"),all.x=TRUE)
temp[is.na(temp)] <- 0
# assign TRUE to active as long as there are some values
temp <- temp %>% mutate(ACTIVE= ifelse(rowSums(temp[,4:ncol(temp)]) > 0, TRUE,FALSE))
#adding attributes of mine
mines <- Mines %>% select(MINE_ID, CURRENT_MINE_NAME, COAL_METAL_IND, CURRENT_MINE_TYPE)
temp <- merge(temp, mines, by = "MINE_ID", all.x=TRUE)
#rearranging columns
temp <- temp[, c(1, 29:ncol(temp), 2:3, 28, 4:27)]
# result
complete_active_quarters <- temp
save(complete_active_quarters, file="./Houston/output/Consolidated.RData")
# see simple_lm.R for simple lm