[This article was first published on Nor Talk Too Wise » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Data must be selected carefully.  The predictive usefulness of the model is grossly diminished if outliers taint the available data.  Figure 1, for instance, shows the Defense spending (as a fraction of the national budget) between 1948 and 1968. Note how the trend curve (as defined by our linear difference model from the last post: see appendix for a fuller description) is a very poor predictor.  Whatever is going on here isn’t a first-order process.  So, let’s neglect the model entirely for a moment.  The huge variations in spending between 1950 and 1952 indicate there were years within the selected time span for which the defense spending dramatically increased because of some exogenous shock, and then spending trended downwards.

One way we can judge the usefulness a little more scientifically is to run a regression on the differences.  In other words, Plot Y(t+1) on top of Y(t), and insert the regression line.  Check it out: In general, the regression is a pretty good fit for the clustered points in the middle.  However, we also have some nasty outliers.  Something about this data isn’t a first-order process.  Wikipedia to the Rescue.  It is history that inconveniently interrupts our model, causing these outliers.  President Harry Truman cut back military spending in the wake of World War II.  However, any hope Truman may have had for shifting the national focus away from foreign military affairs was ruined by the onset of the Korean War late in 1950.  Thus we see the large spike in spending evident in Figure 1.  A predictive first-order model which spans this event with just these data will be limited in its effectiveness.  The grossly underfitted line of Figure 1 is little better than useless for indicating the spending during any given year.  A much more effective model would start by following the onset of the Korean War (a completely unpredictable event if prediction were solely based on these data), and trace the evolution of the expenditure as it decreased from its start-of-conflict high in 1952, as we see in Figure 3. It ain’t perfect, but it’s definitely much better.  So, dynamic modeling with first-order linear difference equations has an enormous array of applications.  However, it is easy to be seduced by the numbers without carefully considering the data and the substantive implications of these findings.  Any inattentiveness in this respect may easily lead to meaningless, incorrect, or downright silly conclusions.

Next Time: Saving the code, changing the method.

References:

Appendix:

This is the R code to generate these Graphs:

df <- read.csv(file="http://nortalktoowise.com/code/datasets/Defense.csv", head=TRUE, sep=",")
attach(df)
lagvar <- function(x,y){return(c(rep(NA, y),x[-((length(x)-y+1):length(x))]))}
lagvar1 <- lagvar(defensespending,1)
model <- lm(defensespending ~ lagvar1)
y2 <- 0
t <- 0
y1 <- .3
a <- model$coefficients[] b <- model$coefficients[]
timeserieslength <- nrow(df)
for (i in 1:timeserieslength) {
y2[i] <- (a*y1[i])+b
t[i] <- i
if (i < timeserieslength) y1[i+1]=y2[i]}
plot(t, defensespending, xlab="Years", ylab="Defense Spending as Fraction of Budget", main="Figure 1: U.S. Defense Expenditure, 1948-68", pch=19)
lines(t, y2, lwd=2)
windows()
plot(lagvar1, defensespending, xlab="", ylab="", pch=19)
title(xlab="Y", ylab="Y(t+1)", main="Figure 2: Plot of the first differences", cex=1.5, col="black", font=2)
abline(model, lwd=2)
windows()
newdf <- df[which(year>1951, arr.in=TRUE),]
lagvar2 <- lagvar(newdf$defensespending,1) model2 <- lm(newdf$defensespending ~ lagvar2)
y2 <- 0
t <- 0
y1 <- .68
a <- model2$coefficients[] b <- model2$coefficients[]
timeserieslength <- nrow(newdf)
for (i in 1:timeserieslength) {
y2[i] <- (a*y1[i])+b
t[i] <- i
if (i < timeserieslength) y1[i+1]=y2[i]}
plot(newdf$year, newdf$defensespending, xlab="Years", ylab="Defense Spending as Fraction of Budget", main="Figure 3: U.S. Defense Expenditure, 1952-68", pch=19)
lines(newdf\$year, y2, lwd=2)


Please forgive my poor style (reusing variable names and whatnot).  It works, and that’s enough for me at the moment.
{"ft_lang_label":"__label__en","ft_lang_prob":0.86384475,"math_prob":0.92422706,"size":4809,"snap":"2021-43-2021-49","text_gpt3_token_len":1233,"char_repetition_ratio":0.10218523,"word_repetition_ratio":0.10882353,"special_character_ratio":0.24994801,"punctuation_ratio":0.13463627,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98316413,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,3,null,3,null,3,null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-19T10:56:50Z\",\"WARC-Record-ID\":\"<urn:uuid:b0e3dd9a-9b1c-4a6f-b978-cd1dbbee4416>\",\"Content-Length\":\"99312\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b782d854-8b2f-4b2e-887e-b1516dfd8973>\",\"WARC-Concurrent-To\":\"<urn:uuid:cd891250-eeeb-4cb9-8c6f-a0ba443a32bb>\",\"WARC-IP-Address\":\"172.64.135.34\",\"WARC-Target-URI\":\"https://www.r-bloggers.com/2010/06/dynamic-modeling-3-when-the-first-order-difference-model-doesn%E2%80%99t-cut-it/\",\"WARC-Payload-Digest\":\"sha1:YUH2YJVNPSRW3IM7CKI7Q55CIOPCXETL\",\"WARC-Block-Digest\":\"sha1:SRAUNWWFHJNLSSRPL4HRCC4QCBRHYUNK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585265.67_warc_CC-MAIN-20211019105138-20211019135138-00610.warc.gz\"}"}