-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCCU_Stat_analysis.R
187 lines (149 loc) · 5.79 KB
/
CCU_Stat_analysis.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Statistical analysis of MC simulation results
# Massimo Pizzol, Feb 2019
library("reshape2")
library('broom')
getwd()
# Near term first
# Import & format data
mc = read.csv('MCsimulation1000iter20190416_nt.csv', sep = ';')
mc <- mc[,-1] # remove pandas index...
mc <- mc[,sort(names(mc))]
head(mc)
names(mc)
summary(mc)
names(mc) <- c("CH4", "CO(DRM)", "CO(rWGS)",
"DMC(elec)", "DMC(eth)",
"DME(SG)","DMM","EtOH(elec)",
"FA(elec)","FA(hydro)","FT",
"MeOH","PEP")
# Export basic stats for reporting
stats_base <- t(sapply(mc,summary))
stats_sd <- sapply(mc,sd)
stats_nice <- cbind(stats_base, stats_sd)
write.table(stats_nice, "CCU-MC-Stats_nt.txt")
# Plot stripcharts
stripchart(mc[,c(1,2,3,5,6,7,8,9,10,11,12,13)], pch = 16,
method = 'jitter', jitter = 0.2, col = 'red',
main = 'Carbon Footprint (st) - results of MC simulation (kg CO2-eq)', las = 2, vertical = TRUE)
# Perform pairwise tests and export result
d <- mc[,1:13] # reshape data in long format to perform pairwise tests
d <- t(d)
d <- cbind(rownames(d), data.frame(d, row.names=NULL))
colnames(d)[1] <- 'route'
d <- melt(d, id = 'route')
res <- pairwise.wilcox.test(d$value, d$route, p.adj = "bonf")
tidy(res)
write.table(tidy(res), "CCU-MC_wil_nt.txt")
# Calculate percentages of differneces between routes, examples
A <- mc$CH4
B <- mc$"CO(DRM)"
par(mfrow=c(1,1))
hist(A-B, breaks = 100) # as Simapro does
sum((A-B) > 0) / 1000 *100 # perc positive difference (B better than A)
sum((A-B) < 0) / 1000 *100 # perc negative difference (A better than B)
sum((A-B) == 0) / 1000 *100 # perc positive difference (A equal to B)
# Do this for all route pairs
diff_names <- list()
diff_values <- list()
diff_perc <- list()
for (i in 1:dim(mc)[2]){
for (j in 1:dim(mc)[2]){
names <- c(names(mc)[i], names(mc)[j])
diff = mc[,i] - mc[,j]
values <- tidy(summary(diff))
perpos <- sum(diff > 0) / 1000 * 100
perneg <- sum(diff < 0) / 1000 * 100
perequ <- sum(diff == 0) / 1000 * 100
perscal <- round(median(mc[,i]) / median(mc[,j]) * 100, 2) # impact of i compared to j
percs <- c(perpos, perneg, perequ, perscal)
index <- (i-1)*dim(mc)[2]+j
diff_names[[index]] <- names
diff_values[[index]] <- values
diff_perc[[index]] <- percs
}
}
doubles <- c() # this is an index to locate duplicate couples (e.g.Route01-Route02, Route02-Route01, I keep only the first)
for (i in 1:dim(mc)[2]){
for (j in 1:i){
doubles <- append(doubles,dim(mc)[2]*i-dim(mc)[2]+j) # this indexing was clever :)
}
}
diff_res <- cbind(do.call(rbind, diff_names), do.call(rbind, diff_values), do.call(rbind, diff_perc))
names(diff_res)[1:2] <- c('BaseRoute','AltRoute')
names(diff_res)[9:12] <- c('Percpos', 'Percneg', 'Percequ', 'Percscal')
diff_res <- diff_res[-doubles,] # removing duplicates
tail(diff_res, 10) #just a check
write.table(diff_res, "CCU-MC_diff_nt.txt")
#####################################################
## Now the same but with the long term scenario
# Import and format data
mc = read.csv('MCsimulation1000iter20190416_lt.csv', sep = ';')
mc <- mc[,-1] # remove pandas index...
mc <- mc[,sort(names(mc))]
head(mc)
names(mc)
summary(mc)
names(mc) <- c("CH4", "CO(DRM)", "CO(rWGS)",
"DMC(elec)", "DMC(eth)",
"DME(SG)","DMM","EtOH(elec)",
"FA(elec)","FA(hydro)","FT",
"MeOH","PEP")
# Export basic stats for reporting
stats_base <- t(sapply(mc,summary))
stats_sd <- sapply(mc,sd)
stats_nice <- cbind(stats_base, stats_sd)
write.table(stats_nice, "CCU-MC_stats_lt.txt")
# Plot stripcharts
stripchart(mc[,c(1,2,3,5,6,7,8,9,10,11,12,13)], pch = 16,
method = 'jitter', jitter = 0.2, col = 'red',
main = 'Carbon Footprint (lt) - results of MC simulation (kg CO2-eq)', las = 2, vertical = TRUE)
# note how the uncertainties are higher for the lt scenario
# Pairwise tests
d <- mc[,1:13] # reshape data in long format to perform pairwise tests
d <- t(d)
d <- cbind(rownames(d), data.frame(d, row.names=NULL))
colnames(d)[1] <- 'route'
d <- melt(d, id = 'route')
res <- pairwise.wilcox.test(d$value, d$route, p.adj = "bonf")
tidy(res)
write.table(tidy(res), "CCU-MC_wil_lt.txt")
# Calculate percentages of differneces between routes, examples
A <- mc$CH4
B <- mc$"CO(DRM)"
par(mfrow=c(1,1))
hist(A-B, breaks = 100) # as Simapro does
sum((A-B) > 0) / 1000 *100 # perc positive difference (B better than A)
sum((A-B) < 0) / 1000 *100 # perc negative difference (A better than B)
sum((A-B) == 0) / 1000 *100 # perc positive difference (A equal to B)
# Do this for all route pairs
diff_names <- list()
diff_values <- list()
diff_perc <- list()
for (i in 1:dim(mc)[2]){
for (j in 1:dim(mc)[2]){
names <- c(names(mc)[i], names(mc)[j])
diff = mc[,i] - mc[,j]
values <- tidy(summary(diff))
perpos <- sum(diff > 0) / 1000 * 100
perneg <- sum(diff < 0) / 1000 * 100
perequ <- sum(diff == 0) / 1000 * 100
perscal <- round(median(mc[,i]) / median(mc[,j]) * 100, 2) # impact of i compared to j
percs <- c(perpos, perneg, perequ, perscal)
index <- (i-1)*dim(mc)[2]+j
diff_names[[index]] <- names
diff_values[[index]] <- values
diff_perc[[index]] <- percs
}
}
doubles <- c() # this is an index to locate duplicate couples (e.g.Route01-Route02, Route02-Route01, I keep only the first)
for (i in 1:dim(mc)[2]){
for (j in 1:i){
doubles <- append(doubles,dim(mc)[2]*i-dim(mc)[2]+j) # this indexing was clever :)
}
}
diff_res <- cbind(do.call(rbind, diff_names), do.call(rbind, diff_values), do.call(rbind, diff_perc))
names(diff_res)[1:2] <- c('BaseRoute','AltRoute')
names(diff_res)[9:12] <- c('Percpos', 'Percneg', 'Percequ', 'Percscal')
diff_res <- diff_res[-doubles,] # removing duplicates
tail(diff_res, 10) #just a check
write.table(diff_res, "CCU-MC_diff_lt.txt")