Background: I am attempting to determine statistics for ambulances using Apache Commons Math. I am able to do very basic univariate statistics for one ambulance, however I get stuck when I want to determine statistics for all the ambulances in my fleet.
Goal: My goal is to generate a basic result set using JDBC, then parse the information out into statistical information. For example, I would like to take the result set and make it look like a table showing the Ambulance, average for 2014, average for 2015 to be the header. The the table details would show each ambulance and the averages for each header
<table>
<tr><th>ambulance</th><th>average response time for year 2014</th><th>average response time for year 2015</th></tr>
<tr><td>Medic1</td><td>62</td><td>74</td></tr>
<tr><td>Medic2</td><td>83</td><td>79</td></tr>
<tr><td>Medic3</td><td>68</td><td>71</td></tr>
</table>
Attempted pseudocode: The pseudocode would look something like this; 1.) Assign a variable for the calendar year 2014 average response time. 2.) loop through all the ambulances in the resultset if the calendar year is 2014 then calculate an average. 3.) assign a variable for the calendar year 2015 average response time. 4.) loop through all the ambulances and if the calendar year is 2015 then calculate the average. 5.) output the Ambulance, average response time for 2014, average response time for 2015
Comments: This would be a good start. At least the logic and format would be present to do more sophisticated analysis such as determine the differences year over year. But I'm stuck. I'm not sure how to do an iteration over each ambulance to generate the average.
I am able to write SQL queries that would generate averages for each ambulance. But I want to use Apache Commons Math because it offers Skew, Kurtosis and other measures. What you see above this paragraph is a simplified example of something more complex.
Java code:
package EMSResearch;
import java.sql.*;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
public class EMSResearch
{
public static void main(String[] args)
{
Connection conn = null;
Statement stmt = null;
try
{
conn = DriverManager.getConnection("jdbc:sqlserver://MyDatabase;database=Emergencies;integratedsecurity=false;user=MyUserName;password=MyPassword");
stmt = conn.createStatement();
String strSelect = "SELECT EmergencyID, YearOfCall, ResponseTime, Ambulance";
ResultSet rset = stmt.executeQuery(strSelect);
DescriptiveStatistics ds = new DescriptiveStatistics();
/*the following code does the job of generating average response time for Medic1 for year 2015. But I want it to loop through and get all the ambulances for year 2015*/
while (rset.next())
{
if (rset.getString("Ambulance").equals("Medic1") && rset.getInt("YearOfCall") == 2015)
{
String event = rset.getString("I_EventNumber");
int year = rset.getInt("YearOfCall");
int responseTime = rset.getInt("ResponseTime");
String truck = rset.getString("Ambulance");
ds.addValue(responseTime);
}
}
System.out.println("mean average value " + ds.getMean());
} catch (SQLException ex)
{
ex.printStackTrace();
} finally
{