Function Repository Resource:

FiveThirtyEightPresidentialApproval

Source Notebook

Retrieve US presidential approval data from the website FiveThirtyEight

Contributed by: Bob Sandheinrich

ResourceFunction["FiveThirtyEightPresidentialApproval"][pres]

gives approval rating data from fivethirtyeight.com for the US president pres.

ResourceFunction["FiveThirtyEightPresidentialApproval"][pres,prop]

gives the property prop.

ResourceFunction["FiveThirtyEightPresidentialApproval"][]

gives data for the current US president.

Details

The only presidents currently available from FiveThirtyEight are Joe Biden and Donald Trump. Automatic can be used as the value of pres to specify the current president.
ResourceFunction["FiveThirtyEightPresidentialApproval"] retrieves data from fivethirtyeight.com, a prominent aggregator of polling data. Therefore, it requires an internet connection.
The default prop "Latest" returns the latest approval and disapproval values. Other properties return Dataset expressions.
Supported values of prop include:
"Latest"(default) value of latest approval and disapproval averages
"Aggregates"average approval data at many times
"TimeSeries"TimeSeries objects containing approval averages for different respondent types
"Polls"a list of individual polls
Each time ResourceFunction["FiveThirtyEightPresidentialApproval"] is evaluated, the data is reimported and interpreted. This can take up to a minute. It is better to only call ResourceFunction["FiveThirtyEightPresidentialApproval"] every few hours and cache data in between, for instance using Once or storing to a variable.

Examples

Basic Examples (4) 

Get the latest presidential approval average from FiveThirtyEight:

In[1]:=
popularity = ResourceFunction["FiveThirtyEightPresidentialApproval"][]
Out[1]=

Calculate the net approval:

In[2]:=
Subtract @@ popularity
Out[2]=

Get a Dataset with average approval and disapproval data over time for the current president:

In[3]:=
ResourceFunction[
 "FiveThirtyEightPresidentialApproval"][Automatic, "Aggregates"]
Out[3]=

Specify the president and retrieve the most recent approval value:

In[4]:=
ResourceFunction["FiveThirtyEightPresidentialApproval"][
  Entity["Person", "JosephBiden::9g8qp"]]["ApproveEstimate"]
Out[4]=

Use a different president:

In[5]:=
ResourceFunction["FiveThirtyEightPresidentialApproval"][
  Entity["Person", "DonaldTrump::6vv3q"]]["ApproveEstimate"]
Out[5]=

Scope (2) 

For the current president, get time series for each subgroup of respondents and values:

In[6]:=
timeseries = ResourceFunction["FiveThirtyEightPresidentialApproval"][
  Entity["Person", "DonaldTrump::6vv3q"], "TimeSeries"]
Out[6]=

Plot one of the time series:

In[7]:=
DateListPlot[timeseries["Adults", "DisapproveEstimate"]]
Out[7]=

Plot all of the time series in a Dataset:

In[8]:=
timeseries[All, All, DateListPlot]
Out[8]=

Create a labeled plot of all the values for polls of likely and registered voters:

In[9]:=
timeseries["Voters", DateListPlot]
Out[9]=

Get the net approval as a time series:

In[10]:=
net = timeseries[
  "All polls", #ApproveEstimate - #DisapproveEstimate &]
Out[10]=

See the latest value:

In[11]:=
net["LastValue"]
Out[11]=

Plot it:

In[12]:=
DateListPlot[net, Filling -> Axis]
Out[12]=

Get a dataset for individual polls:

In[13]:=
polls = ResourceFunction["FiveThirtyEightPresidentialApproval"][
  Entity["Person", "JosephBiden::9g8qp"], "Polls"]
Out[13]=

Limit the results to only a few well-rated pollsters:

In[14]:=
goodpolls = polls[Select[
   StringContainsQ[#PollsterRatingName, "YouGov" | "Selzer" | "Ipsos" | "Harris"] &]]
Out[14]=

Get the five most recent of these polls:

In[15]:=
recent = goodpolls[TakeLargestBy["EndDate", UpTo@5]]
Out[15]=

Plot timelines for approval values for the six most common pollsters over time:

In[16]:=
polls[GroupBy["PollsterRatingName"]][
 TakeLargestBy[Length, 6]/*DateListPlot,
 TimeSeries[
   Lookup[#, "Approve"], {DateObject /@ Lookup[#, "EndDate"]}] &
 ]
Out[16]=

See the adjusted values accounting for pollster bias:

In[17]:=
polls[GroupBy["PollsterRatingName"]][
 TakeLargestBy[Length, 6]/*DateListPlot,
 TimeSeries[
   Lookup[#, "ApproveAdjusted"], {DateObject /@ Lookup[#, "EndDate"]}] &
 ]
Out[17]=

Possible Issues (1) 

Presidents before Donald Trump are not currently available:

In[18]:=
ResourceFunction[
 "FiveThirtyEightPresidentialApproval"]["James Madison"]
Out[18]=

Publisher

Bob

Version History

  • 1.2.0 – 03 April 2024
  • 1.1.0 – 29 January 2021
  • 1.0.0 – 29 May 2020

Related Resources

Author Notes

Version 1.2.0: Updated existing tools for changes in 538's data structure.

License Information