@@ -94,31 +94,100 @@ scoreTemporalDQ <- function(data_year,target_year=NA, scoring_bounds) {
9494 return (score )
9595}
9696
97+ # ' Creates a 3d matrix of data quality scores (flow by sector)
98+ # ' @param model A complete EEIO model object with TbS
99+ # ' @return A 3d matrix, third dimension is the individual data quality indicator
97100createB_dqi <- function (model ) {
98- # modeled from standardizeandcastSatelliteTable()
99101 df <- model $ TbS
100102 df [, " Sector" ] <- apply(df [, c(" Sector" , " Location" )],
101103 1 , FUN = joinStringswithSlashes )
102- # Cast df into a flow x sector matrix
103- df_cast <- reshape2 :: dcast(df , Flow ~ Sector , fun.aggregate = sum , value.var = " TemporalCorrelation" )
104- # Move Flow to rowname so matrix is all numbers
105- rownames(df_cast ) <- df_cast $ Flow
106- df_cast $ Flow <- NULL
107- df_cast [, setdiff(model $ Industries $ Code_Loc , colnames(df_cast ))] <- 0
108- # Adjust column order to be the same with V_n rownames
109- df_cast <- df_cast [, model $ Industries $ Code_Loc ]
110- dqi <- as.matrix(df_cast )
104+
105+ dq_fields <- getDQfields(df )
106+ # Default score is 5
107+ df [dq_fields ] <- lapply(df [dq_fields ], function (x ) ifelse(is.na(x ), 5 , x ))
108+ # Get unique Flow and Sector values
109+ flows <- unique(df $ Flow )
110+ sectors <- model $ Industries $ Code_Loc
111+
112+ # Initialize 3D array
113+ dqi_3d <- array (5 , dim = c(length(flows ), length(sectors ), length(dq_fields )),
114+ dimnames = list (Flow = flows , Sector = sectors , Variable = dq_fields ))
111115
112- # # Need to Transform into a flow x commodity matrix using market shares matrix for commodity models?
113- # # see createBfromFlowDataandOutput()
116+ # Fill the array
117+ for (i in seq_along(dq_fields )) {
118+ df_cast <- reshape2 :: dcast(df , Flow ~ Sector , value.var = dq_fields [i ], fun.aggregate = sum , fill = 5 )
119+ df_cast [, setdiff(model $ Industries $ Code_Loc , colnames(df_cast ))] <- 5
120+ # Adjust column order to be the same with V_n rownames
121+ df_cast <- df_cast [, model $ Industries $ Code_Loc ]
122+ dqi_3d [,,i ] <- as.matrix(df_cast )
123+ }
124+
125+ # # Transform into a flow x commodity matrix using market shares matrix for commodity models
114126 if (model $ specs $ CommodityorIndustryType == " Commodity" ) {
115- dqi <- dqi %*% model $ V_n
116- # ^^ is this correct? needs to reassign scores across commodities instead of industries
117- colnames(dqi ) <- model $ Commodities $ Code_Loc
127+ transformed_3d <- array (5 , dim = c(length(flows ), length(model $ Commodities $ Code_Loc ), length(dq_fields )),
128+ dimnames = list (Flow = flows , Sector = model $ Commodities $ Code_Loc , Variable = dq_fields ))
129+ for (i in seq_along(dq_fields )) {
130+ transformed_3d [,,i ] <- dqi_3d [,,i ] %*% model $ V_n
131+ }
132+ dqi_3d <- transformed_3d
118133 }
134+ # TODO round values
119135
120- # array(combined_vector, dim = c(5, 5, 5))
121-
122-
123- return (dqi )
136+ return (dqi_3d )
124137}
138+
139+ # ' Helper function to create a 3d array for dqi matrices
140+ # ' @param dqi A complete EEIO model object with TbS
141+ # ' @param num_matrices, int of number of matrices (e.g. 5)
142+ # ' @param name_matrices, name of each matrix
143+ # ' @return A 3d matrix, of correct dimensions
144+ initializeArray <- function (dqi , num_matrices , name_matrices ) {
145+ dqi_3d <- array (5 , dim = c(nrow(dqi ), ncol(dqi ), num_matrices ),
146+ dimnames = list (rows = rownames(dqi ),
147+ columns = colnames(dqi ),
148+ Variable = name_matrices ))
149+ return (dqi_3d )
150+ }
151+
152+ # ' Creates D_dqi matrix from B_dqi using the formula
153+ # ' (C %*% (B * B_dqi)) / D
154+ # ' @param model A complete EEIO model object.
155+ # ' @return A 3d matrix of dqi scores for D
156+ createDdqi <- function (model ) {
157+ B_dqi <- model $ B_dqi
158+ D_dqi <- (model $ C %*% (model $ B * B_dqi [,,1 ])) / model $ D # Temporary for initialization
159+ dqi_3d <- initializeArray(D_dqi , dim(B_dqi )[3 ], dimnames(B_dqi )[[3 ]])
160+ for (i in seq_along(dim(B_dqi )[3 ])) {
161+ dqi_3d [,,i ] <- (model $ C %*% (model $ B * B_dqi [,,i ])) / model $ D
162+ }
163+ return (dqi_3d )
164+ }
165+
166+ # ' Creates M_dqi matrix from B_dqi using the formula
167+ # ' ((B * B_dqi) %*% L) / M
168+ # ' @param model A complete EEIO model object.
169+ # ' @return A 3d matrix of dqi scores for M
170+ createMdqi <- function (model ) {
171+ B_dqi <- model $ B_dqi
172+ M_dqi <- ((model $ B * B_dqi [,,1 ]) %*% model $ L ) / model $ M # Temporary for initialization
173+ dqi_3d <- initializeArray(M_dqi , dim(B_dqi )[3 ], dimnames(B_dqi )[[3 ]])
174+ for (i in seq_along(dim(B_dqi )[3 ])) {
175+ dqi_3d [,,i ] <- ((model $ B * B_dqi [,,i ]) %*% model $ L ) / model $ M
176+ }
177+ return (dqi_3d )
178+ }
179+
180+ # ' Creates N_dqi matrix from D_dqi using the formula
181+ # ' ((D * D_dqi) %*% L) / N
182+ # ' @param model A complete EEIO model object.
183+ # ' @return A 3d matrix of dqi scores for N
184+ createNdqi <- function (model ) {
185+ D_dqi <- model $ D_dqi
186+ N_dqi <- ((model $ D * D_dqi [,,1 ]) %*% model $ L ) / model $ N # Temporary for initialization
187+ dqi_3d <- initializeArray(N_dqi , dim(D_dqi )[3 ], dimnames(D_dqi )[[3 ]])
188+ for (i in seq_along(dim(D_dqi )[3 ])) {
189+ dqi_3d [,,i ] <- ((model $ D * D_dqi [,,i ]) %*% model $ L ) / model $ N
190+ }
191+ return (dqi_3d )
192+ }
193+
0 commit comments