# 1.1 load the same household data used in the Lab7_Script.R file, create the object `HTS`
library(data.table)
library(foreign)
HTS <- data.table(read.spss("datasets/HTS.household.10regions.sav",to.data.frame = T))
# 2. Recreate the same steps used in the lab to run a T-test, but instead, consider the following:
# 2.1 Run a T-Test to show if the household income means is statistically different between households living in single family residences or not (use the whole sample). Produce two pdfs, one with an histogram pdf plot, and another with the simulated hypothesis testing plot showing where the T-statistic falls. Provide a short interpretation of your results
test <- HTS[,t.test(formula = hhincome ~ sf)]
library(ggplot2)
ggplot(data=HTS,aes(x=hhincome))+
geom_histogram()+
facet_grid(sf~.) +
labs(title = "Household Income by Single Family or Other")
ggsave("~/Desktop/UTSA URP Work/Spring 2024/URP 5393/Labs/07-lab-nmolinasatx/hhincome_histogram.pdf", plot = )
curve(dt(x, df = 4353), from = -40, to = 40)
abline(h=0,col='blue')
points(x=test$statistic,y=0,col='red')
# the test statistic of -36.234 falls beyond the p-value, indicating that we can reject the null hypothesis. There is a significant difference between household incomes in single-family homes vs income in other types of households.
# 2.2 Filter the sample to select only the region of San Antonio. Prepare an T-Test to show if the household vehicle miles traveled (in natural logs - lnvmt) is statistically different between households living in neighborhoods with a job-population index (variable `jobpop`) over and under the city median (of the `jobpop` variable of course)
hts_sa<-HTS[region%in%c("San Antonio, TX")]
median(hts_sa$jobpop)
hts_sa[,jobpop:=as.numeric(jobpop>=0.6457728)]
hts_sa[,job_pop_median:=factor(jobpop,levels = c(0,1),labels = c("under median","over median"))]
sa_test <- hts_sa[, t.test(formula = lnvmt ~ job_pop_median)]
hts_sa[, t.test(formula = lnvmt ~ job_pop_median)]
# 2.2 using the same data set (San Antonio sample), run an ANOVA test to see if there are significant differences between income categories and vehicle miles traveled by household. Follow the same steps used in the ANOVA exercise done in class. Produce three pdfs: one checking the normality assumption of the dependent variable, a second one checking the presence of outliers, and a third one showing the Tukey (post hoc) T-tests plot.
# checking normality of dependent variable - the vehicle miles traveled
hist(hts_sa$lnvmt)
# checking for outliers
ggplot(data=hts_sa, aes(x=income_cat, y= lnvmt))+
geom_boxplot()
# deleting outliers
hts_sa_bp<-boxplot(hts_sa$lnvmt~hts_sa$income_cat)
outliers <- hts_sa_bp$out
hts_sa[lnvmt%in%outliers,]
hts_sa2<-hts_sa[!lnvmt%in%outliers,]
boxplot(hts_sa$lnvmt~hts_sa$income_cat)
boxplot(hts_sa2$lnvmt~hts_sa2$income_cat)
# Tukey plot
fit<-aov(hts_sa2$lnvmt~hts_sa2$income_cat)
summary(fit)
TukeyHSD(fit)
plot(TukeyHSD(fit))
# 2. [30 points] Run a T-test and an ANOVA test in your data.
# for this section, I am using the HTS data to explore the relationship between household income and transit use
# first, I will run a t-test to see if there is a significant difference in income between households who take any sort of transit and those that do not
HTS <- data.table(read.spss("datasets/HTS.household.10regions.sav",to.data.frame = T))
hhincome_transit_test <- HTS[,t.test(formula = hhincome ~ anytransit)]
# with a t value of 5.6055 and a tiny p-value of 2.647e-08, we can reject the null hypothesis that there is no significant difference in means. This means that there is a significant difference in mean income in households that use any transit versus those that do not
# next, I will perform an ANOVA test to see if there are significant differences in the number of transit trips per household by income category
hist(HTS$transittrips)
ggplot(data=HTS, aes(x=income_cat, y= transittrips))+
geom_boxplot()
aov(HTS$transittrips~HTS$income_cat)
fit <- aov(HTS$transittrips~HTS$income_cat)
summary(fit)
TukeyHSD(fit)
plot(TukeyHSD(fit))
# the p-values of the ANOVA tests are very small. It appears that there are significant differences in the number of transit trips per household by income category.
# Bonus: [30 points] Provide an HTML file with your answers using R-Markdown.
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.