This analysis is available in both R and Python. You can access either version using the tab.

Note: this is a minimal example provided for teaching purposes. At some point, I’ll get around to presenting a more extensive analysis.

R Analysis

The Nationscape data have data from many cross-sectional waves. For simplicity (and arbitrarily), we’ll look at 3 time points (could be more).

Wave 1: ns20190718

Wave 41: ns20200423

Wave 68: ns20201029

Packages and setup

library(tidyverse)
library(lubridate)
library(rio)

Importing the Data

Import the data.

w1 <- rio::import("Nationscape-Weekly-Materials-DTA-2021Dec/phase_1_v20210301/ns20190718/ns20190718.dta")

w2 <- rio::import("Nationscape-Weekly-Materials-DTA-2021Dec/phase_2_v20210301/ns20200423/ns20200423.dta")

w3 <- rio::import("Nationscape-Weekly-Materials-DTA-2021Dec/phase_3_v20210301/ns20201029/ns20201029.dta")

### Add a wave indicator

w1$wave <- "w1"
w2$wave <- "w2"
w3$wave <- "w3"

Right Track Wrong Track

### pull the data

# write a little function to calculate proportions in each wave

props <- function(wave){
  
  the_wave=wave$wave[1]
  Wrong_track_prop <- length((which(wave$right_track==2)))/dim(wave)[1]
  right_track_prop <- length((which(wave$right_track==1)))/dim(wave)[1]

  thing <- tribble(
    
    ~"Wave", ~"Wrong Track",~"Right Track",
    the_wave,Wrong_track_prop,right_track_prop
  )
  
  return(thing)
}

the_data <- bind_rows(props(w1),
          props(w2),
          props(w3))

### summarise and plot

the_data %>% 
  pivot_longer(!Wave,names_to="Direction",values_to="Proportion") %>% 
  group_by(Wave,Direction) %>% 
  ggplot(aes(y=Proportion,x=Wave,group=Direction,color=Direction))+
  geom_line()+
  ylim(0,1)

Python Analysis

Import the data in Python and Wrangle

import pandas as pd





w1=pd.read_stata("Nationscape-Weekly-Materials-DTA-2021Dec/phase_1_v20210301/ns20190718/ns20190718.dta")

w2=pd.read_stata("Nationscape-Weekly-Materials-DTA-2021Dec/phase_2_v20210301/ns20200423/ns20200423.dta")

w3=pd.read_stata("Nationscape-Weekly-Materials-DTA-2021Dec/phase_3_v20210301/ns20201029/ns20201029.dta")

### Add a wave indicator

w1['wave']="w1"
w2['wave']="w2"
w3['wave']="w3"

def props(wave):
  
  the_wave=wave['wave'][1]
  
  right_track_prop=wave[wave['right_track']=="Generally headed in the right direction"].shape[0]/wave.shape[0]
  
  wrong_track_prop=wave[wave['right_track']=="Off on the wrong track"].shape[0]/wave.shape[0]

  the_data=pd.DataFrame({"Wave":the_wave,"Right Track":right_track_prop,"Wrong Track":wrong_track_prop},index=[0])
  
  return(the_data)

w1_dat=props(w1)
w2_dat=props(w2)
w3_dat=props(w3)

fig_dat=pd.concat([props(w1),props(w2),props(w3)])

fig_dat_long=fig_dat.melt(id_vars='Wave')

Make a figure


import matplotlib.pyplot as plt
## if you need to, go to the R chunk and run conda_install("matplotlib")

fig, ax = plt.subplots( nrows=1, ncols=1 )
ax.plot (fig_dat['Wave'], fig_dat['Right Track']) 
ax.plot (fig_dat['Wave'], fig_dat['Wrong Track']) 
plt.show()