# Another mortgage rates animated gif

IN THE PAST I’ve told you how I made my mortgage rates gif. In this post I’m make an extension that uses stop motion techniques to reverse course. We’ll end up with this:

For reference, here’s the standard gif I share each Thursday after mortgage rates come out:

# Stop motion animation

While thinking about the week-to-week movements in rates it’s easy to lose longer-term perspective. Rates in the week of May 12, 2016 were the lowest in three years. I decided to tweak my animation to give that perspective.

In order to do so I thought I’d first roll forward in time from the beginning of 2016 and then start rolling backward in time. For the standard gif I fix the scales so that you aren’t disoriented by movement. But when going backward in time I let the scales dynamically adjust so you can feel the range of data change. I also added some rough easing effects by changing the number of weeks between frames, speeding up and slowing down the animation as we move through time.

## The code

Here’s the R script to enable the animation:

{% highlight r #Load some data stored in a text file called “pmms30yr” #these data have one column of dates, one column of rates, and helper columns called year and week (week number) pmms30yr <- fread(“pmms30yr.txt”) pmms30yr$date<-as.Date(pmms30yr$date, format="%m/%d/%Y")

# Note, I did some counting to figure out which weeks I needed for the animation. Not elegant, but works.

#Run the animation

#Partition data: 2016 YTD, since May 2013, 5 years prior oopt = ani.options(interval = 0.01) saveGIF({ #First standard forward animation (slowed to half place by pausing in J: for (i in 1:19) { for (j in 1:2){ g<-myplotf(pmms30yr[year==2016 & week <=i])+ #Add labels and caption, use subtitle for annotations labs(x="", y="", title=“30-year Fixed Mortgage Rate (%)”, subtitle=“Mortgage rates decline throughout 2016 reaching a low on May 12…”, caption="@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey") ani.pause()} } #Pause for drama for (i2 in 1:8) { print(g) ani.pause() }

# Start moving backwards to three years ago:

for (i in 1:137) { g<- myplotf(pmms30yr[date>= dd13[157-i]])+ labs(x="", y="", title=“30-year Fixed Mortgage Rate (%)”, subtitle="…rates haven’t been this low in three years…", caption="@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey") #annotate(“text”, x=pmms30yr[year==2016 & week==1]\$date, y=3.5, #label="…rates haven’t been\n that low in\n three years.",color=“red”,hjust=0) print(g) ani.pause() } #Pause for more drama for (i2 in 1:8) { print(g) ani.pause() } #Start moving backwards:

# Then slow down (13 weeks) to (6 weeks) to finally 1 week

for (i in c(seq(1,365,6),seq(378,716,13),seq(1145,1574,26),seq(1587,2003,13),seq(2016,2185,6),2196) ) { g<- myplotf(pmms30yr[date>= dd[2353-156-i]])+ labs(x="", y="", title=“30-year Fixed Mortgage Rate (%)”, subtitle="…and rates are very low by historical standards", caption="@lenkiefer Source: Freddie Mac Primary Mortgage Market Survey") print(g) ani.pause() } for (i2 in 1:8) { print(g) ani.pause() } },movie.name=“rate_5_14_2016.gif”,ani.width = 575, ani.height = 450) {% endhighlight

And the result: