%% Accelerate Exploratory Programming Using the Live Editor
% The following is an example of how to use the Live Editor to accelerate exploratory programming. This example demonstrates how you can use the Live Editor to:
% See output together with the code that produced it.
% Divide your program into sections to evaluate blocks of code individually.
% Include visualizations.
% Experiment with parameter values using controls.
% Summarize and share your findings.
%
%% Load Highway Fatality Data
% The Live Editor displays output together with the code that produced it. To run a section, go to the Live Editor tab and select the Run Section button. You can also click the blue bar that appears when you move your mouse to the left edge of a section.
% In this example, we explore some highway fatality data. Start by loading the data. The variables are shown as the column headers of the table.

load fatalities
fatalities(1:10,:)

%
%% Calculate Fatality Rates
% The Live Editor allows you to divide your program into sections containing text, code, and output. To create a new section, go to the Live Editor tab and click the Section Break button. The code in a section can be run independently, which makes it easy to explore ideas as you write your program.
% Calculate the fatality rate per one million vehicle miles. From these values we can find the states with the lowest and highest fatality rates.

states = fatalities.Properties.RowNames;
rate = fatalities.deaths./fatalities.vehicleMiles;
[~, minIdx] = min(rate);                  % Minimum accident rate
[~, maxIdx] = max(rate);                  % Maximum accident rate
disp([states{minIdx} ' has the lowest fatality rate at ' num2str(rate(minIdx))])
disp([states{maxIdx} ' has the highest fatality rate at ' num2str(rate(maxIdx))])

%
%% Distribution of Fatalities
% You can include visualizations in your program. Like output, plots and figures appear together with the code that produced them.
% We can use a bar chart to see the distribution of fatality rates among the states. There are 11 states that have a fatality rate greater than 0.02 per million vehicle miles.

histogram(rate,10)
xlabel('Fatalities per Million Vehicle Miles')
ylabel('Number of States')

%
%% Find Correlations in the Data
% You can explore your data quickly in the Live Editor by experimenting with parameter values to see how your results will change. Add controls to change parameter values interactively. To add controls, go to the Live Editor tab, click the Control button, and select from the available options.
% We can experiment with the data to see if any of the variables in the table are correlated with highway fatalities. For example, it appears that highway fatality rates are lower in states with a higher percentage urban population.

dataToPlot = "urbanPopulation";
close                                      % Close any open figures
scatter(fatalities.(dataToPlot),rate)         % Plot fatalities vs. selected variable
xlabel(dataToPlot)
ylabel('Percent Fatalities per Million Vehicle Miles') 

hold on
xmin = min(fatalities.(dataToPlot));
xmax = max(fatalities.(dataToPlot));
p = polyfit(fatalities.(dataToPlot),rate,1);  % Calculate & plot least squares line
plot([xmin xmax], polyval(p,[xmin xmax]))

%
%% Plot Fatalities and Urbanization on a US Map
% Summarize your results and share your live script with your colleagues. Using your live script, they can recreate and extend your analysis. You can also save your analysis as HTML, Microsoft® Word, or PDF documents for publication.
% Based on this analysis, we can summarize our findings using a plot of fatality rates and urban population on a map of the continental United States.

load usastates.mat
figure
geoplot([usastates.Lat], [usastates.Lon], 'black')
geobasemap darkwater
hold on
geoscatter(fatalities.latitude,fatalities.longitude,2000*rate,fatalities.urbanPopulation,'filled')
c = colorbar;
title(c,'Percent Urban')

% Copyright 2012 The MathWorks, Inc.
%