library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag():    dplyr, stats
library(maptools) 
## Loading required package: sp
## Checking rgeos availability: TRUE
library(rgdal) 
## Warning: package 'rgdal' was built under R version 3.4.2
## rgdal: version: 1.2-13, (SVN revision 686)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.1.3, released 2017/20/01
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-5
library(RgoogleMaps) # googlemaps API tool
library(classInt) 
library(RColorBrewer) 
library(ggplot2); library(ggmap) 
library(maps);library(mapdata)
## 
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
## 
##     map
library(rgeos)
## rgeos version: 0.3-23, (SVN revision 546)
##  GEOS runtime version: 3.6.1-CAPI-1.10.1 r0 
##  Linking to sp version: 1.2-4 
##  Polygon checking: TRUE
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 3.4.2
census_api_key(Sys.getenv("CENSUS_API_KEY"))
## To install your API key for use in future sessions, run this function with `install = TRUE`.
library(tigris) # to pull census shapefiles from the web API
## As of version 0.5.1, tigris does not cache downloaded data by default. To enable caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
## 
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
## 
##     plot
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3

Bring in the raw file from the BARI shared Google Drive and take a look at it.

a <- read.csv("~/Google Drive/BARI/BARI Research Team Data Library/Miscellaneous/Somerville/somer_happiness_2013_cleaned.csv",stringsAsFactors = F)
a_combined <- read.csv("~/Google Drive/BARI/BARI Research Team Data Library/Miscellaneous/Somerville/2011and2013.csv",stringsAsFactors = F)
head(a)
##                          Match_addr.C.120  X.N.19.6 Y.N.19.6
## 1     40 Laurel St, Somerville, MA, 02143 -71.10427 42.38468
## 2      16 Cedar St, Somerville, MA, 02143 -71.11495 42.38866
## 3 270 Highland Ave, Somerville, MA, 02143 -71.11113 42.39161
## 4     75 Myrtle St, Somerville, MA, 02145 -71.08326 42.38449
## 5     110 Pearl St, Somerville, MA, 02145 -71.08661 42.38528
## 6   73 Pearson Ave, Somerville, MA, 02144 -71.11129 42.39697
##   Disp_Lon.N.19.6 Disp_Lat.N.19.6 SurveyID
## 1       -71.10460        42.38480        1
## 2       -71.11520        42.38878        2
## 3       -71.11129        42.39136        3
## 4       -71.08276        42.38466        4
## 5       -71.08674        42.38504        5
## 6       -71.11112        42.39689        6
##   How.happy.do.you.feel.right.now.
## 1                               10
## 2                                3
## 3                                8
## 4                                6
## 5                                5
## 6                                8
##   How.satisfied.are.you.with.your.life.in.general.
## 1                                               10
## 2                                                2
## 3                                                8
## 4                                                6
## 5                                                5
## 6                                                8
##   How.satisfied.are.you.with.Somerville.as.a.place.to.live.
## 1                                                         1
## 2                                                         7
## 3                                                         8
## 4                                                         1
## 5                                                         5
## 6                                                         8
##   How.satisfied.are.you.with.your.neighborhood.
## 1                                            10
## 2                                             6
## 3                                             8
## 4                                             5
## 5                                             5
## 6                                             6
##   How.would.you.rate.the.following..The.beauty.or.physical.setting.of.Somerville
## 1                                                                              3
## 2                                                                              3
## 3                                                                              3
## 4                                                                              3
## 5                                                                              4
## 6                                                                              4
##   How.would.you.rate.the.following..The.cost.of.housing
## 1                                                     5
## 2                                                     2
## 3                                                     5
## 4                                                     3
## 5                                                     3
## 6                                                     3
##   How.would.you.rate.the.following..The.overall.quality.of.public.schools
## 1                                                                       0
## 2                                                                       0
## 3                                                                       0
## 4                                                                       0
## 5                                                                       0
## 6                                                                       0
##   How.would.you.rate.the.following..The.effectiveness.of.the.local.police
## 1                                                                       5
## 2                                                                       4
## 3                                                                       5
## 4                                                                       3
## 5                                                                       3
## 6                                                                       4
##   How.would.you.rate.the.following..The.maintenance.of.streets..sidewalks..and.squares
## 1                                                                                    4
## 2                                                                                    3
## 3                                                                                    3
## 4                                                                                    2
## 5                                                                                    3
## 6                                                                                    4
##   How.would.you.rate.the.following..The.availability.of.social.community.events.
## 1                                                                              3
## 2                                                                              4
## 3                                                                              4
## 4                                                                              4
## 5                                                                              3
## 6                                                                              5
##   How.safe.do.you.feel.walking.in.your.neighborhood.at.night.
## 1                                                          10
## 2                                                           7
## 3                                                           9
## 4                                                           4
## 5                                                           5
## 6                                                           6
##   How.satisfied.are.you.with.the.beauty.or.physical.setting.of.your.neighborhood.
## 1                                                                               6
## 2                                                                               6
## 3                                                                               5
## 4                                                                               3
## 5                                                                               5
## 6                                                                               6
##   How.satisfied.are.you.with.the.appearance.of.parks.in.your.neighborhood.
## 1                                                                        8
## 2                                                                        6
## 3                                                                        4
## 4                                                                        8
## 5                                                                        6
## 6                                                                        6
##   Gender..Male1.   Age Hispanic              Ethnicity HaveKids
## 1              0 45-54        0                  White        1
## 2              1 25-34        0                  White        0
## 3              0 35-44        0                  Asian        1
## 4              1 55-64        0                  White        0
## 5              1 45-54        0 Black/African American        1
## 6              1 25-34        0                  White        0
##   PlanMove2Years How_long_lived_Somerville             Income
## 1              1          21 years or more   $150,000 or more
## 2              1               11-15 years $50,000 to $74,999
## 3              1                 4-7 years $50,000 to $74,999
## 4              0          21 years or more  Less than $10,000
## 5              1               11-15 years $25,000 to $49,999
## 6              0          Less than a year $75,000 to $99,999
##               Neighborhood AreStudent Side.C.1 Addr_type.C.20
## 1                                   0        L        Address
## 2            Porter Square          0        L        Address
## 3 Somerville Hospital Area          0        L        Address
## 4            75 Myrtle St.          0        R        Address
## 5                Pearl St.          0        L        Address
## 6              Ball Square          0        R        Address
##    ARC_Addres.C.100 ARC_City.C.40 ARC_State.C.20
## 1      40 Laurel St    Somerville             MA
## 2      16 Cedar St.    Somerville             MA
## 3 270 Highland Ave.    Somerville             MA
## 4     75 Myrtle St.    Somerville             MA
## 5     110 Pearl St.    Somerville             MA
## 6   73 Pearson Ave.    Somerville             MA

Basic summary stats:

ggplot(a) + 
      geom_histogram(aes(How.happy.do.you.feel.right.now.),binwidth=1) + 
      labs(x="Happiness at the moment")

ggplot(a) + 
      geom_histogram(aes(How.satisfied.are.you.with.Somerville.as.a.place.to.live.),binwidth=1) + 
      labs(x="Happiness with Somerville as a place to live")

a %>% 
      group_by(Ethnicity) %>%
      summarize(avghappiness = mean(How.satisfied.are.you.with.Somerville.as.a.place.to.live.),
                n = length(How.satisfied.are.you.with.Somerville.as.a.place.to.live.))
## # A tibble: 9 x 3
##                                 Ethnicity avghappiness     n
##                                     <chr>        <dbl> <int>
## 1                                             6.571429    14
## 2                         American Indian     1.000000     1
## 3                                   Asian     7.476190    21
## 4                  Black/African American     8.500000     4
## 5 Black/African American, Native American    10.000000     1
## 6                                   White     7.775510   147
## 7                            White, Asian     6.500000     2
## 8           White, Black/African American     8.333333     3
## 9    White, Black/African American, Asian     8.000000     1
ggplot(a) + 
      geom_histogram(aes(How.satisfied.are.you.with.your.neighborhood.),binwidth=1) + 
      labs(x="Satisfaction with neighborhood")

Now for some spatial analyses.

Looks like there is an odd extra set of long/lat coords in the data. I’m guessing those are projected coordinates or something similar from another program used to plot geo data.

Going to drop extra coordinates and other variables that aren’t useful here.

a_new <- a %>%
      select("Match_addr.C.120",
             "X.N.19.6", "Y.N.19.6",
             "How.satisfied.are.you.with.Somerville.as.a.place.to.live.",
             "How.satisfied.are.you.with.your.neighborhood.",
             "How.happy.do.you.feel.right.now.",
             "Gender..Male1.","Age","Hispanic","Ethnicity","HaveKids","PlanMove2Years",
             "How_long_lived_Somerville","Income","Neighborhood","AreStudent")

In order to do any spatial things with these data, we need to turn into a spatial points dataframe:

happiness.spdf <- SpatialPointsDataFrame(coords = cbind(a_new$X.N.19.6,a_new$Y.N.19.6),
                                               data = a_new,
                                               proj4string=CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0") # these data are just Long/Lat coords
                                               )

Bring in geographic reference shapefiles and subset them to Somerville area:

Map the Census geographies:

somer_base <- get_map(location="Somerville, MA",zoom=13, color="color",maptype="terrain",crop=T)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=Somerville,+MA&zoom=13&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Somerville,%20MA&sensor=false
b0 <- ggmap(somer_base, extent="device",legend="none",maprange=T)
## Scale for 'x' is already present. Adding another scale for 'x', which
## will replace the existing scale.
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.
## Warning: `panel.margin` is deprecated. Please use `panel.spacing` property
## instead
(b1 <- b0 + 
      geom_polygon(aes(x=long,y=lat,group=group),data=somer_bg,color = "black",alpha=0.4))
## Regions defined for each Polygons

(b1 <- b0 + 
      geom_polygon(aes(x=long,y=lat,group=group),data=somer_tracts,color = "black",alpha=0.4))
## Regions defined for each Polygons

Look at happiness within tracts:

tracts_overlay <- over(happiness.spdf,somer_tracts)

happiness.spdf2 <- cbind(data.frame(happiness.spdf), tracts_overlay) # attach this data about tracts (polygons) back to each row of point data

# then summarize point data within each tract:
tract_summary <- group_by(happiness.spdf2,GEOID) %>%
                       summarize(
                    avg_happiness_somer = mean(How.satisfied.are.you.with.Somerville.as.a.place.to.live.,na.rm=T),
                    avg_satis_neigh = mean(How.satisfied.are.you.with.your.neighborhood.,na.rm=T),
                    avg_happiness = mean(How.happy.do.you.feel.right.now.,na.rm=T),
                    n_happiness = length(!is.na(How.happy.do.you.feel.right.now.))
)
somer_tracts@data$avg_happiness <- tract_summary$avg_happiness[match(somer_tracts@data$GEOID,tract_summary$GEOID)]
somer_tracts@data$avg_satis_neigh <- tract_summary$avg_satis_neigh[match(somer_tracts@data$GEOID,tract_summary$GEOID)]
somer_tracts@data$avg_happiness_somer <- tract_summary$avg_happiness_somer[match(somer_tracts@data$GEOID,tract_summary$GEOID)]

somer_tracts@data$id <- rownames(somer_tracts@data)
somer_tracts2 <- fortify(somer_tracts,region="id") # separates polygons into individual vertices
somer_tracts2 <- plyr::join(somer_tracts2,somer_tracts@data,by="id")
(b2 <- b0 + 
      geom_polygon(aes(x=long,y=lat,group=group,fill = avg_happiness_somer),data=somer_tracts2,alpha=0.8) +
            labs(fill="Avg. Happiness with Somerville"))

This makes it definitely appear that the East Somerville and Tufts areas are less happy, while the Davis Square and Inman Square areas are the happiest.

But what about their satisfaction with their own neighborhood?

(b3 <- b0 + 
      geom_polygon(aes(x=long,y=lat,group=group,fill = avg_satis_neigh),data=somer_tracts2,alpha=0.8) +
            labs(fill="Avg. Happiness with neighborhood"))

Here it looks like East Somerville and Tufts areas also unhappy with their neighborhood. Likely pretty collinear with Somerville satisfaction, though. Also, Inman Square still looks super satisfied.

What about average happiness - not necessarily with their surroundings - as an underlying latent sentiment?

(b4 <- b0 + 
      geom_polygon(aes(x=long,y=lat,group=group,fill = avg_happiness),data=somer_tracts2,alpha=0.8) +
            labs(fill="Avg. Happiness at moment"))

Here, it looks like East Somerville definitely is where people are least happy - at least momentarily. Could confound other measurements if we believe this is an underlying sentiment.

Output the tract-level data for other uses:

somer_tracts@data$fips_code <- paste0(somer_tracts@data$STATEFP,somer_tracts@data$COUNTYFP,somer_tracts@data$TRACTCE)
write.csv(somer_tracts@data,"somerville_happiness_tracts.csv",row.names = F)