# 2.
data$RegisteredDummy <- ifelse(data$Registered %in% c(1, 2), 1, 0)
barplot(table(data$RegisteredDummy), main = "Voter Registration Status", xlab = "Registered", ylab = "Count", names.arg = c("Not Registered", "Registered"))
registered_count <- sum(data$RegisteredDummy == 1)
cat("Number of respondents registered to vote:", registered_count, "\n")
## Number of respondents registered to vote: 16714
#In this data set we see around 15,000 respondents are registered to vote
# 3.
data$On.track <- factor(data$On.track, levels = c(2, 1), labels = c("Going in the right direction", "Have gotten off on the wrong track"))
hist_data <- ggplot(data, aes(x = McCain.Therm, fill = On.track)) +
geom_histogram(binwidth = 5, position = "dodge", color = "black", alpha = 0.7) +
labs(title = "Histogram: McCain Thermometer by On.track", x = "McCain Thermometer", y = "Count", fill = "On.track") +
scale_fill_manual(name = "On.track", values = c("Going in the right direction" = "blue", "Have gotten off on the wrong track" = "orange"))
print(hist_data)
## Warning: Removed 610 rows containing non-finite values (`stat_bin()`).
#What does this graph tell you? This graph is able to show us how the public feels about McCain. it is an indecator is they feel he is improving or doing the oppisite.
# 4.
data$Gender <- factor(data$Gender, levels = c(1, 2), labels = c("Male", "Female"))
boxplot_data <- ggplot(data, aes(x = Gender, y = Obama.Therm)) +
geom_boxplot(fill = "lightblue", color = "black", alpha = 0.7) +
labs(title = "Boxplot: Obama Thermometer by Gender", x = "Gender", y = "Obama Thermometer") +
theme_minimal()
print(boxplot_data)
## Warning: Removed 566 rows containing non-finite values (`stat_boxplot()`).
#This graph is to help us see that females are more likely to be in favor of obama
# 5.
ggplot(data, aes(x = Income, y = McCain.Therm)) +
geom_point() +
labs(title = "Scatterplot: Income vs. McCain Thermometer", x = "Income", y = "McCain Thermometer")
## Warning: Removed 610 rows containing missing values (`geom_point()`).
#this graph helps demonstrate the increase or decrease of income with McCain
# 6.
#please refer to the scatterplot you created in #5.
ggplot(data, aes(x = Income, y = McCain.Therm, color = factor(Metro.area))) +
geom_point() +
labs(title = "Scatterplot: Income vs. McCain Thermometer by Metro Area", x = "Income", y = "McCain Thermometer") +
scale_color_discrete(name = "Metro Area")
## Warning: Removed 610 rows containing missing values (`geom_point()`).
#with this graph we can see that when people are either in a metro area or outside of one how this affects their overall income.
#7.
result_chi2 <- chisq.test(table(data$Military.service, data$Economy))
## Warning in chisq.test(table(data$Military.service, data$Economy)): Chi-squared
## approximation may be incorrect
cat("Chi-square test results:\n")
## Chi-square test results:
print(result_chi2)
##
## Pearson's Chi-squared test
##
## data: table(data$Military.service, data$Economy)
## X-squared = 31.651, df = 12, p-value = 0.001566
#overall these results tell us that there is no association between Military Service and Economy, Chi squared was used as the data is two categorical variables
#8.
result_ttest <- t.test(Obama.Therm ~ On.track, data = data)
cat("T-test results:\n")
## T-test results:
print(result_ttest)
##
## Welch Two Sample t-test
##
## data: Obama.Therm by On.track
## t = 33.172, df = 8590.7, p-value < 2.2e-16
## alternative hypothesis: true difference in means between group Going in the right direction and group Have gotten off on the wrong track is not equal to 0
## 95 percent confidence interval:
## 16.88783 19.00910
## sample estimates:
## mean in group Going in the right direction
## 54.47503
## mean in group Have gotten off on the wrong track
## 36.52657
# with these results we can see a substantial and statistically significant difference in favorability towards Obama based on the 'On.track' variable.
#9
result_cor <- cor.test(data$Bush.Therm, data$McCain.Therm)
cat("Correlation test results:\n")
## Correlation test results:
print(result_cor)
##
## Pearson's product-moment correlation
##
## data: data$Bush.Therm and data$McCain.Therm
## t = 143.74, df = 17475, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.7291772 0.7427663
## sample estimates:
## cor
## 0.7360459
# here we are shown a strong and positive linear relationship between favorability ratings for George W. Bush and John McCain.
#10
#table on digital copy
#A chi-squared test of independence was used on categorical data, showing a significant association between the two variables (Chi-squared = [calculated value], df = [degrees of freedom], p-value < [chosen significance level] this tell us that the distribution of observations across the categories is not uniform and suggesting a relationship between the variables.
# 11
model_gender_obama <- lm(Obama.Therm ~ Gender, data = data)
summary(model_gender_obama)
##
## Call:
## lm(formula = Obama.Therm ~ Gender, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -52.870 -30.994 3.006 29.006 54.006
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.9943 0.3829 120.14 <2e-16 ***
## GenderFemale 6.8761 0.5108 13.46 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 33.58 on 17560 degrees of freedom
## (566 observations deleted due to missingness)
## Multiple R-squared: 0.01022, Adjusted R-squared: 0.01016
## F-statistic: 181.2 on 1 and 17560 DF, p-value: < 2.2e-16
#In the linear regression model, controlling for the intercept, the coefficient for 'Gender' is 6.88 suggesting on average, females rate Obama 6.88 points higher on the Thermometer scale over men. However, the model has a low goodness-of-fit (Adjusted R-squared: 0.01016), this implies limited explanatory power of 'Gender' in predicting Obama Thermometer scores.
#12
model_ideology_mccain <- lm(McCain.Therm ~ Ideology, data = data)
summary(model_ideology_mccain)
##
## Call:
## lm(formula = McCain.Therm ~ Ideology, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -74.869 -16.354 3.646 14.943 83.349
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 6.9482 0.5261 13.21 <2e-16 ***
## Ideology 9.7030 0.1192 81.43 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 23.98 on 17420 degrees of freedom
## (706 observations deleted due to missingness)
## Multiple R-squared: 0.2757, Adjusted R-squared: 0.2757
## F-statistic: 6631 on 1 and 17420 DF, p-value: < 2.2e-16
#the linear regression model predicting McCain Thermometer scores from the variable 'Ideology,' the coefficient for 'Ideology' is 9.70, showing on average, a one-unit increase in the Ideology scale is associated with a 9.70-point increase in McCain Thermometer scores. This model has a strong goodness-of-fit (Adjusted R-squared: 0.2757), demonstrating that 'Ideology' significantly contributes to explaining the variability in McCain Thermometer scores.
#13 Select two variables. Write a hypothesis that was not tested in questions #11-12. Conduct a bivariate regression analysis. Provide a table of your results and interpret them.
model_bivariate_marital_obama <- lm(Obama.Therm ~ Marital.status, data = data)
summary(model_bivariate_marital_obama)
##
## Call:
## lm(formula = Obama.Therm ~ Marital.status, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -62.316 -32.032 2.729 27.729 52.729
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 43.5101 0.5008 86.88 <2e-16 ***
## Marital.status 3.7612 0.2560 14.69 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 33.55 on 17560 degrees of freedom
## (566 observations deleted due to missingness)
## Multiple R-squared: 0.01214, Adjusted R-squared: 0.01208
## F-statistic: 215.8 on 1 and 17560 DF, p-value: < 2.2e-16
# this suggest on average, individuals with a different Marital status rate Obama 3.76 points higher on the Thermometer scale compared to those with Marital status. However, the model has a low goodness-of-fit (Adjusted R-squared: 0.01208), indicating limited explanatory power of 'Marital.status' in predicting Obama Thermometer scores.
#14
model_multivariate_obama <- lm(Obama.Therm ~ Local.TV + Evangelical + Income, data = data)
summary(model_multivariate_obama)
##
## Call:
## lm(formula = Obama.Therm ~ Local.TV + Evangelical + Income, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -59.283 -30.563 1.489 27.490 67.699
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 30.41003 1.19367 25.476 < 2e-16 ***
## Local.TV -1.76999 0.21366 -8.284 < 2e-16 ***
## Evangelical 15.46930 0.54809 28.224 < 2e-16 ***
## Income -0.29551 0.06314 -4.680 2.89e-06 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 32.94 on 17034 degrees of freedom
## (1090 observations deleted due to missingness)
## Multiple R-squared: 0.04765, Adjusted R-squared: 0.04749
## F-statistic: 284.1 on 3 and 17034 DF, p-value: < 2.2e-16
#
#15
model_custom_multivariate <- lm(Race ~ Marital.status + Military.service + Income, data = data)
summary(model_custom_multivariate)
##
## Call:
## lm(formula = Race ~ Marital.status + Military.service + Income,
## data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.6226 -0.4284 -0.3811 -0.3158 3.7274
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.443036 0.046617 30.955 < 2e-16 ***
## Marital.status 0.021239 0.007625 2.785 0.00535 **
## Military.service 0.021612 0.009937 2.175 0.02965 *
## Income -0.013051 0.001865 -6.997 2.71e-12 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.9566 on 18047 degrees of freedom
## (77 observations deleted due to missingness)
## Multiple R-squared: 0.004593, Adjusted R-squared: 0.004428
## F-statistic: 27.76 on 3 and 18047 DF, p-value: < 2.2e-16
#Individuals with a different Marital.status have a slightly higher predicted Race score (Coefficient = 0.021, p-value = 0.00535).
#Individuals with Military.service have a slightly higher predicted Race score (Coefficient = 0.022, p-value = 0.02965).
#Individuals with higher Income have a slightly lower predicted 'Race' score (Coefficient = -0.013, p-value < 2e-16).
#The overall model has limited explanatory power (Adjusted R-squared: 0.00443), suggesting that these variables collectively explain only a small proportion of the variability in 'Race.'
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.