Module: OpenTox::Validation::RegressionStatistics
- Included in:
- RegressionCrossValidation, RegressionLeaveOneOut, RegressionTrainTest
- Defined in:
- lib/validation-statistics.rb
Overview
Statistical evaluation of regression validations
Instance Method Summary collapse
-
#correlation_plot(format: "png") ⇒ Blob
Plot predicted vs measured values.
-
#percent_within_prediction_interval ⇒ Float
Get percentage of measurements within the prediction interval.
-
#statistics ⇒ Hash
Get statistics.
-
#worst_predictions ⇒ Hash
Get predictions with measurements outside of the prediction interval.
Instance Method Details
#correlation_plot(format: "png") ⇒ Blob
Plot predicted vs measured values
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/validation-statistics.rb', line 172 def correlation_plot format: "png" unless correlation_plot_id tmpfile = "/tmp/#{id.to_s}_correlation.#{format}" x = [] y = [] feature = Feature.find(predictions.first.last["prediction_feature_id"]) predictions.each do |sid,p| x << p["measurements"].median y << p["value"] end R.assign "measurement", x R.assign "prediction", y R.eval "all = c(measurement,prediction)" R.eval "range = c(min(all), max(all))" if feature.name.match /Net cell association/ # ad hoc fix for awkward units title = "log2(Net cell association [mL/ug(Mg)])" else title = feature.name title += " [#{feature.unit}]" if feature.unit and !feature.unit.blank? end R.eval "image = qplot(prediction,measurement,main='#{title}',xlab='Prediction',ylab='Measurement',asp=1,xlim=range, ylim=range)" R.eval "image = image + geom_abline(intercept=0, slope=1)" R.eval "ggsave(file='#{tmpfile}', plot=image)" file = Mongo::Grid::File.new(File.read(tmpfile), :filename => "#{id.to_s}_correlation_plot.#{format}") plot_id = $gridfs.insert_one(file) update(:correlation_plot_id => plot_id) end $gridfs.find_one(_id: correlation_plot_id).data end |
#percent_within_prediction_interval ⇒ Float
Get percentage of measurements within the prediction interval
165 166 167 |
# File 'lib/validation-statistics.rb', line 165 def percent_within_prediction_interval 100*within_prediction_interval.to_f/(within_prediction_interval+out_of_prediction_interval) end |
#statistics ⇒ Hash
Get statistics
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 |
# File 'lib/validation-statistics.rb', line 113 def statistics self.warnings = [] self.rmse = 0 self.mae = 0 self.within_prediction_interval = 0 self.out_of_prediction_interval = 0 x = [] y = [] predictions.each do |cid,pred| if pred[:value] and pred[:measurements] x << pred[:measurements].median y << pred[:value] error = pred[:value]-pred[:measurements].median self.rmse += error**2 self.mae += error.abs if pred[:prediction_interval] if pred[:measurements].median >= pred[:prediction_interval][0] and pred[:measurements].median <= pred[:prediction_interval][1] self.within_prediction_interval += 1 else self.out_of_prediction_interval += 1 end end else trd_id = model.training_dataset_id smiles = Compound.find(cid).smiles self.warnings << "No training activities for #{smiles} in training dataset #{trd_id}." $logger.debug "No training activities for #{smiles} in training dataset #{trd_id}." end end R.assign "measurement", x R.assign "prediction", y R.eval "r <- cor(measurement,prediction,use='pairwise')" self.r_squared = R.eval("r").to_ruby**2 self.mae = self.mae/predictions.size self.rmse = Math.sqrt(self.rmse/predictions.size) $logger.debug "R^2 #{r_squared}" $logger.debug "RMSE #{rmse}" $logger.debug "MAE #{mae}" $logger.debug "#{percent_within_prediction_interval.round(2)}% of measurements within prediction interval" $logger.debug "#{warnings}" save { :mae => mae, :rmse => rmse, :r_squared => r_squared, :within_prediction_interval => within_prediction_interval, :out_of_prediction_interval => out_of_prediction_interval, } end |
#worst_predictions ⇒ Hash
Get predictions with measurements outside of the prediction interval
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/validation-statistics.rb', line 204 def worst_predictions worst_predictions = predictions.select do |sid,p| p["prediction_interval"] and p["value"] and (p["measurements"].max < p["prediction_interval"][0] or p["measurements"].min > p["prediction_interval"][1]) end.compact.to_h worst_predictions.each do |sid,p| p["error"] = (p["value"] - p["measurements"].median).abs if p["measurements"].max < p["prediction_interval"][0] p["distance_prediction_interval"] = (p["measurements"].max - p["prediction_interval"][0]).abs elsif p["measurements"].min > p["prediction_interval"][1] p["distance_prediction_interval"] = (p["measurements"].min - p["prediction_interval"][1]).abs end end worst_predictions.sort_by{|sid,p| p["distance_prediction_interval"] }.to_h end |