Save your API key to your working directory

use census_api_key(key = "yourkeyhere", install = T)

one time to install your key for use in tidycensus

library(tidycensus)
census_api_key(key ="f67be5a86e696eebf0c84a5a20d85f2c04f7e9ef", overwrite= T  )
## To install your API key for use in future sessions, run this function with `install = TRUE`.
options(tigris_use_cache = TRUE)

Load libraries (new)

library (car)
## Loading required package: carData
library(brms)
## Loading required package: Rcpp
## Loading required package: ggplot2
## Loading 'brms' package (version 2.6.0). Useful instructions
## can be found by typing help('brms'). A more detailed introduction
## to the package is available through vignette('brms_overview').
## Run theme_set(theme_default()) to use the default bayesplot theme.
library(pscl)
## Classes and Methods for R developed in the
## Political Science Computational Laboratory
## Department of Political Science
## Stanford University
## Simon Jackman
## hurdle and zeroinfl functions by Achim Zeileis
library(rstan)
## Loading required package: StanHeaders
## rstan (Version 2.18.1, GitRev: 2e1f913d3ca3)
## For execution on a local, multicore CPU with excess RAM we recommend calling
## options(mc.cores = parallel::detectCores()).
## To avoid recompilation of unchanged Stan programs, we recommend calling
## rstan_options(auto_write = TRUE)
library(haven)
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Extract from ACS housing data for 2010 for TX Census Tracts that includes outcome variable (median housing value) and predictor variables (poverty, education, employment)

tx_acs<-get_acs(geography = "tract", state="TX", year = 2010,
                variables=c( "DP04_0080PE", "DP03_0002PE", "DP02_0061PE", "DP03_0119PE"), geometry = T, output = "wide")
## Getting data from the 2006-2010 5-year ACS
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
## Getting data from the 2006-2010 5-year ACS
## Using the ACS Data Profile
names(tx_acs)
##  [1] "GEOID"       "NAME"        "DP04_0080PE" "DP04_0080PM" "DP03_0002PE"
##  [6] "DP03_0002PM" "DP02_0061PE" "DP02_0061PM" "DP03_0119PE" "DP03_0119PM"
## [11] "geometry"

Regression model with 3 predictors

#create a county FIPS code - 5 digit

tx_acs$county<-substr(tx_acs$GEOID, 1, 5)

#rename variables and filter missing cases
tx_acs2<-tx_acs%>%
  mutate(homevalue=DP04_0080PE, laborforce=DP03_0002PE, hsgrad=DP02_0061PE, poverty= DP03_0119PE) %>%
#  st_transform(crs = 102740)%>%
  filter(complete.cases(homevalue, laborforce, hsgrad, poverty))

class(tx_acs2)
## [1] "sf"         "tbl_df"     "tbl"        "data.frame"
tx_acs2$homevaluez<-scale(tx_acs2$homevalue, center = T, scale = T)
fit1<-glm(homevaluez~laborforce+hsgrad+poverty, data=tx_acs2)
summary(fit1)
## 
## Call:
## glm(formula = homevaluez ~ laborforce + hsgrad + poverty, data = tx_acs2)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1155  -0.4382  -0.0954   0.3192   5.2757  
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.6206761  0.0938537   6.613 4.14e-11 ***
## laborforce  -0.0259764  0.0011470 -22.648  < 2e-16 ***
## hsgrad       0.0252939  0.0011599  21.807  < 2e-16 ***
## poverty      0.0285581  0.0009438  30.257  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.5769714)
## 
##     Null deviance: 5149.0  on 5149  degrees of freedom
## Residual deviance: 2969.1  on 5146  degrees of freedom
## AIC: 11789
## 
## Number of Fisher Scoring iterations: 2
confint(fit1)
## Waiting for profiling to be done...
##                   2.5 %      97.5 %
## (Intercept)  0.43672619  0.80462601
## laborforce  -0.02822442 -0.02372835
## hsgrad       0.02302050  0.02756720
## poverty      0.02670824  0.03040804
options(mc.cores=parallel::detectCores())
fit2<-brm(homevaluez~laborforce+hsgrad+poverty, data=tx_acs2, family=gaussian(),
          warmup=1000, #burnin for 500 interations for each chain = 1000 burnin
              iter=5000, chains=2, #2*5000 = 10000 - 1000 burnin = 9000 total iterations
              cores=2,seed = 1115, #number of computer cores, 1 per chain is good.
            refresh = 0)
## Compiling the C++ model
## Start sampling
summary(fit1)
## 
## Call:
## glm(formula = homevaluez ~ laborforce + hsgrad + poverty, data = tx_acs2)
## 
## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -3.1155  -0.4382  -0.0954   0.3192   5.2757  
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.6206761  0.0938537   6.613 4.14e-11 ***
## laborforce  -0.0259764  0.0011470 -22.648  < 2e-16 ***
## hsgrad       0.0252939  0.0011599  21.807  < 2e-16 ***
## poverty      0.0285581  0.0009438  30.257  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for gaussian family taken to be 0.5769714)
## 
##     Null deviance: 5149.0  on 5149  degrees of freedom
## Residual deviance: 2969.1  on 5146  degrees of freedom
## AIC: 11789
## 
## Number of Fisher Scoring iterations: 2
print(summary(fit2), digits = 3)
##  Family: gaussian 
##   Links: mu = identity; sigma = identity 
## Formula: homevaluez ~ laborforce + hsgrad + poverty 
##    Data: tx_acs2 (Number of observations: 5150) 
## Samples: 2 chains, each with iter = 5000; warmup = 1000; thin = 1;
##          total post-warmup samples = 8000
## 
## Population-Level Effects: 
##            Estimate Est.Error l-95% CI u-95% CI Eff.Sample  Rhat
## Intercept     0.620     0.093    0.438    0.802       8896 1.000
## laborforce   -0.026     0.001   -0.028   -0.024       8897 1.000
## hsgrad        0.025     0.001    0.023    0.028       9840 1.000
## poverty       0.029     0.001    0.027    0.030       9109 1.000
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Eff.Sample  Rhat
## sigma    0.760     0.007    0.746    0.774       4031 1.000
## 
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample 
## is a crude measure of effective sample size, and Rhat is the potential 
## scale reduction factor on split chains (at convergence, Rhat = 1).

There is similarity between the two models but the Bayesian shows more significant effects.

Specify alternative priors to gauge robusticity

prior1<-c(set_prior("normal(0,1)", class="b"),#prior for the beta's
         set_prior("normal(0,1)", class="Intercept"), #prior for the intercept
         set_prior("inv_gamma(.5,.5)", class="sigma"))#prior for the residual std. deviation
fit3<-brm(homevaluez~laborforce+hsgrad+poverty, data=tx_acs2, family=gaussian(),
              prior = prior1, 
              warmup=1000, #burnin for 1000 interations for each chain = 1000 burnin
              iter=5000, chains=2, #2*5000 = 10000 - 2000 burnin = 8000 total iterations
              cores=2,seed = 1115, #number of computer cores, 1 per chain is good.
              save_model="fit_lm_gamma_stan.txt", refresh = 0
            )
## Compiling the C++ model
## Start sampling

95% Bayesian credible interval

out1<-as.data.frame(fit2)
out2<-as.data.frame(fit3)

mysum<-function(x){
  mu=mean(x,na.rm=T)
  sdev=sd(x,na.rm=T)
  lci=quantile(x, p=.025,na.rm=T)
  uci=quantile(x, p=.975,na.rm=T)
list(mu=mu, sd=sdev, lci=lci, uci=uci)
  }

sapply(out1, mysum )
##     b_Intercept b_laborforce b_hsgrad    b_poverty    sigma      
## mu  0.6199188   -0.02596662  0.02530446  0.02855602   0.7597098  
## sd  0.09342329  0.001144094  0.001175192 0.0009483787 0.007366503
## lci 0.4376479   -0.02820969  0.022976    0.02668773   0.7455745  
## uci 0.802344    -0.02377191  0.02761464  0.03042585   0.7741612  
##     lp__     
## mu  -5898.08 
## sd  1.555595 
## lci -5901.918
## uci -5895.993
sapply(out2, mysum)
##     b_Intercept b_laborforce b_hsgrad    b_poverty   sigma       lp__     
## mu  0.6204675   -0.02598039  0.02529288  0.02856393  0.7596457   -5896.956
## sd  0.0915618   0.00111319   0.001155704 0.000924755 0.007553085 1.571746 
## lci 0.4370923   -0.02819525  0.02299183  0.02676528  0.7451818   -5900.851
## uci 0.803352    -0.02374401  0.02755481  0.03038992  0.7746515   -5894.889

90% Bayesian credible interval

out3<-as.data.frame(fit2)
out4<-as.data.frame(fit3)

mysum<-function(x){
  mu=mean(x,na.rm=T)
  sdev=sd(x,na.rm=T)
  lci=quantile(x, p=.05,na.rm=T)
  uci=quantile(x, p=.95,na.rm=T)
list(mu=mu, sd=sdev, lci=lci, uci=uci)
  }

sapply(out3, mysum )
##     b_Intercept b_laborforce b_hsgrad    b_poverty    sigma      
## mu  0.6199188   -0.02596662  0.02530446  0.02855602   0.7597098  
## sd  0.09342329  0.001144094  0.001175192 0.0009483787 0.007366503
## lci 0.4662746   -0.02785142  0.02338406  0.02698353   0.7478034  
## uci 0.7747336   -0.02406206  0.02725169  0.03010183   0.7717638  
##     lp__     
## mu  -5898.08 
## sd  1.555595 
## lci -5901.082
## uci -5896.167
sapply(out4, mysum)
##     b_Intercept b_laborforce b_hsgrad    b_poverty   sigma       lp__     
## mu  0.6204675   -0.02598039  0.02529288  0.02856393  0.7596457   -5896.956
## sd  0.0915618   0.00111319   0.001155704 0.000924755 0.007553085 1.571746 
## lci 0.4710233   -0.02781957  0.02338992  0.0270524   0.7473473   -5899.935
## uci 0.7702039   -0.02413734  0.02721156  0.03007382  0.7721869   -5895.047

Specify alterntive priors

#Student's T regression model
prior<-c(set_prior("normal(0,1)", class="b"),#prior for the beta's
         set_prior("cauchy(0,5)", class="sigma"),#prior for the residual std. deviation
         set_prior("gamma(1,1.5)", class="nu")) #prior for the t distribution d.f.

fit4<-brm(homevaluez~laborforce+hsgrad+poverty, data=tx_acs2, family=student(),
              prior = prior, 
              warmup=1000, 
              iter=5000, chains=2, 
              cores=2,seed = 1115,
              save_model="fit4_stan.txt", refresh=0)
## Compiling the C++ model
## Start sampling
print(summary(fit4), digits=3)
##  Family: student 
##   Links: mu = identity; sigma = identity; nu = identity 
## Formula: homevaluez ~ laborforce + hsgrad + poverty 
##    Data: tx_acs2 (Number of observations: 5150) 
## Samples: 2 chains, each with iter = 5000; warmup = 1000; thin = 1;
##          total post-warmup samples = 8000
## 
## Population-Level Effects: 
##            Estimate Est.Error l-95% CI u-95% CI Eff.Sample  Rhat
## Intercept     0.659     0.083    0.500    0.820      10359 1.000
## laborforce   -0.026     0.001   -0.028   -0.024      10362 1.000
## hsgrad        0.022     0.001    0.020    0.024       9522 1.000
## poverty       0.024     0.001    0.022    0.026       7963 1.000
## 
## Family Specific Parameters: 
##       Estimate Est.Error l-95% CI u-95% CI Eff.Sample  Rhat
## sigma    0.487     0.010    0.467    0.507       2597 1.000
## nu       2.903     0.153    2.618    3.220       2693 1.000
## 
## Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample 
## is a crude measure of effective sample size, and Rhat is the potential 
## scale reduction factor on split chains (at convergence, Rhat = 1).
WAIC1<-WAIC(fit2)
WAIC2<-WAIC(fit4)
compare_ic(WAIC1, WAIC2, ic=c("waic"))
##                 WAIC     SE
## fit2        11792.25 173.14
## fit4        10985.95 151.40
## fit2 - fit4   806.30  95.10

In this comparison, the normal is not preferred and the more complicated model is better.

WAIC(fit2, fit3, compare = T)
##                 WAIC     SE
## fit2        11792.25 173.14
## fit3        11792.06 173.23
## fit2 - fit3     0.19   0.12