Compare Player Salaries by Injury Status

This example shows how to retrieve salary and injury data from STATS.com for individual baseball players. To compare salaries of players with and without injuries, visualize the data in a scatter plot.

This example requires a STATS.com API key and secret pass code for baseball data. For details about your credentials for data access, contact STATS.com.

To access the code for this example, enter edit MLBSalaryVsInjuryExample.m.

Connect to STATS.com

Create a STATS.com connection sBaseball for retrieval of statistical baseball data for the sport league named 'mlb'. Set up the connection to retrieve salary data by specifying the query parameter 'Resource' as 'salaries'.

sBaseball = statsllc('gkfrq6fxabcehmn2yctrc6j5','qYR5abCQgc', ...
    'DataType','stats','LeagueAbbreviation','mlb', ...
    'Resource','salaries','SportName','baseball', ...
    'VersionNumber','v1');

Retrieve Baseball Player Salary and Injury Data

Retrieve individual baseball player salaries data using sBaseball.

data = fetch(sBaseball);

To retrieve the individual baseball player data playerData, access the nested structure data. Create a salary data table salaryData by retrieving the structure fields in playerData.

playerData = [data.apiResults.league.season.salaries.player];

salaryData = table((1:data.recordCount)',[playerData.playerId]', ...
    {playerData.firstName}',{playerData.lastName}', ...
    [data.apiResults.league.season.salaries.salary]', ...
    false(data.recordCount,1), ...
    'VariableNames',{'Record','PlayerId','FirstName', ...
    'LastName','Salary','Injured'});

salaryData contains these column names:

  • Record — Record number

  • PlayerId — Baseball player identifier

  • FirstName — Baseball player first name

  • LastName — Baseball player last name

  • Salary — Baseball player salary

  • Injured — Baseball player injured status

Baseball player injured status is a placeholder for the injury data.

To retrieve the injury data, change the query parameter Resource value from 'salaries' to 'participants' in the STATS.com connection sBaseball. Retrieve the individual baseball player data using sBaseball.

pause(2) 
sBaseball.Resource = 'participants';

data = fetch(sBaseball);

Loop through the resulting nested structure data for each player. Set the injured status in salaryData for those players that are injured.

numPlayers = data.recordCount;
for i = 1:numPlayers
    playerId = data.apiResults.league.players{i}.playerId;
    salaryData.Injured(salaryData.PlayerId == playerId) ...
        = data.apiResults.league.players{i}.isInjured;
end

Visualize Baseball Player Salary and Injury Data

Create a scatter plot of the salary for each baseball player.

sInjured = scatter(salaryData.Record(salaryData.Injured), ...
    salaryData.Salary(salaryData.Injured),'Marker','+', ...
    'Linewidth',2);
hold on
sNotInjured = scatter(salaryData.Record(~salaryData.Injured), ...
    salaryData.Salary(~salaryData.Injured),'Marker','O', ...
    'Linewidth',2);
hold off
sInjured.MarkerFaceColor = 'flat'
grid on
b = gca
title('Player Salary Comparison')
xlabel('Record')
ylabel('Salary')
ytickformat(b,'usd')
legend({'Player with Injury','Player Without Injury'},'Location','best')

The scatter plot of baseball players and their salaries displays. Compare the salaries for injured and uninjured baseball players.

See Also

|

Related Topics

External Websites