Animating by year with GIF in Markdown

To animate by year, try the code below. Give the code chunk the eval=FALSE option so that Rmarkdown does not try to run this while knitting.

anim_speed <- 3
anim_duration <- (max(all_country_data$Year) - 
                min(all_country_data$Year) + 1) / 
                anim_speed

animate(animation1, 
        fps = anim_speed,
        duration = anim_duration)

anim_save(here("scripts", "life_gdp.gif")

Afterward, include the animation in your markdown using Markdown to include an image. Start with ! followed by [] with the alternate text and () with the file path. Note that the animation should be stored in the same folder as the Rmd file for this to work. If you move the resulting html, copy the animation file to the same location.

![Life GDP Animation](life_gdp.gif)

Loading and saving Census data

We don’t want to query the Census servers every time we run our code, so it’s best to download data as follows: Check to see if the data already exists. If not, download it and save it as an RDS file. Then, load the RDS file.

countiesRDS <- here("data_public", "counties_vermont.RDS")
if(!file.exists(countiesRDS)){
   vermont_counties <- counties(state = "Vermont", cb=TRUE)
   saveRDS(vermont_counties, countiesRDS) 
}
vermont_counties <- readRDS(countiesRDS)

Notebook for lab

Rmd Notebook

Tmap

Tmap is awesome for mapping because:

  • view mode creates a dynamic Leaflet map
  • plot mode that creates a static graph
  • Syntax has the feel of ggplot
  • Classifying data is easy
  • You can add more than one data layer
  • Incredibly customizable

Get started! with data on hurricane tracks from the International best track archive for climate stewardship

Rendered Animation

Life GDP Animation

Spatial Correlation

Tobler’s first law of geography states that “everything is related to everything else, but near things are more related than distant things”. This is spatial dependence, a.k.a. spatial autocorrelation, and it suggests that some degree of spatial clustering is to be expected. However, researchers are often interested in whether spatial clustering is unusually pronounced. Are there significant spatial hotspots of vehicle break-ins? Cases of leukemia? Earthquakes? High college exam scores? UFO sightings? Social media posts about a conspiracy theory?
Spatial statistics can help answer these questions, both globally (is clustering present in the region of study? yes or no?) and locally (is clustering present in a particular location? Where are significant clusters located?)

Getis Ord

One of the first tests students learn in statistics is the T-test to determine whether two samples have different means.

The Getis Ord statistic is a spatial version of the T-test, in which one sample is a local neighborhood and the other sample is everywhere else. The test is calculated for every location on the map, and reports whether the neighborhood around each location has significantly higher or lower values than everywhere else. In other words, it uses statistics to find hot spots.

Defining ``local’’

A Spatial Weights Matrix formally defines ``neighborhoods’’ for the purposes of spatial statistics. There are many ways to define a neighborhood, but the most common ways are to use a defined distance radius (e.g. every geographic unit within 20 miles) or topological adjacency (e.g. every geograhpic unit with a common boundary).

References

The following references on spatial statistics are not required reading: they are references for the theoretical/technical details of spatial cluster detection.

Amusing