getwd() First we need to load the data into r

states<-read.csv("C:/Users/jedia/Desktop/RWD/nes2020_subset-1.csv")

We’re going to convert our variable into a value.

taxrich<-states$tax_rich

Next we need to convert the value into a table, so that we can make a barplot with it.

tax<-table(taxrich)
tax
## taxrich
##   Favor Neutral  Oppose 
##    4793    1394    1197

Now we just need to create a barplot for our table.

barplot(tax, col="Dark Red", ylab="Frequency", main="Perception on Increasing Taxes on the Rich")

Now we need to calculate the confidence interval for this variable

First, we’re going to assign numeric values to Agree, Neutral, and Oppose. We’ll make it an even interval, with Agree being 1 and Oppose being 0.

Agree<-rep(1, 4793)
Neutral<-rep(0, 1394)
Oppose<-rep(0, 1197)

Next we have to group these numeric values into a single data set

tax_data<-c(Agree, Neutral, Oppose)

Then we calculate the mean

mean_tax<-mean(tax_data)
mean_tax
## [1] 0.6491062

Next we calculate the standard deviation

sd_tax<-sd(tax_data)
sd_tax
## [1] 0.4772821

We need to make a variable for the sample size in order to get the standard error

n_tax<-4793+1394+1197
n_tax
## [1] 7384

We now calculate the standard error using the standard deivation and the sample size

se_tax<-sd_tax/sqrt(n_tax)
se_tax
## [1] 0.0055543

We’ll use a confidence level of 95%, which we’ll use to determine the confidence interval

We’re going to set up a variable for what we will be adding or subtracting from the mean, using 2 and the standard error

PoM_tax<-2*se_tax
PoM_tax
## [1] 0.0111086

Now we just have to add and subtract the variable we created to the mean, in order to get the confidence interval

CIup_tax<-mean_tax+PoM_tax
CIup_tax
## [1] 0.6602148
CIlow_tax<-mean_tax-PoM_tax
CIlow_tax
## [1] 0.6379976

Now we’re going to make our economic mobility graph.

mobility<-states$econ_mobility

Next, we need to make a table for mobility, so that we can turn that into a barplot

econ<-table(mobility)
barplot(econ)

The barplot is in a very confusing order, so we need to change the order of the columns in the data.

We do this by using c(), and then writing the preferred order for the variables. Then we run that with the initial table, and it adjust the data.

order_econ<-c("A great deal easier", "Moderately Easier", "Little easier", "Same", "A little harder", "Moderately harder","A great deal harder")
ordered_econ<-econ[order_econ]
barplot(ordered_econ)

As we can see, the barplot now goes from easier to harder. Now we just need to clean it up using some basic commands.

barplot(ordered_econ, col="dark green", main="Perceptions on Economic Mobility", ylab="Frequency")

Now we need to calculate the confidence interval, just following the same steps as last time

We need to make even intervals for 7 variables this time as well.

gde<-rep(0, 565)
me<-rep(.166, 517)
le<-rep(.333, 283)
same<-rep(.5, 1501)
lh<-rep(.666, 1116)
mh<-rep(.833, 1227)
gdh<-rep(1, 2170)
econ_data<-c(gde, me, le, same, lh, mh, gdh)
mean_econ<-mean(econ_data)
mean_econ
## [1] 0.6594265
sd_econ<-sd(econ_data)
sd_econ
## [1] 0.3142456
n_econ<-(565+517+283+1501+1116+1227+2170)
n_econ
## [1] 7379
se_econ<-sd_econ/sqrt(n_econ)
se_econ
## [1] 0.003658225
PoM_econ<-2*se_econ
PoM_econ
## [1] 0.007316451
CIUp_econ<-mean_econ+PoM_econ
CIUp_econ
## [1] 0.6667429
CILow_econ<-mean_econ-PoM_econ
CILow_econ
## [1] 0.65211