**Nor Talk Too Wise » R**, and kindly contributed to R-bloggers)

(This is the first in a series on the use of Graph Algebraic models for Social Science.)

Linear Difference models are a hugely important first step in learning Graph Algebraic modeling. That said, linear difference equations are a completely independent thing from Graph Algebra. I’ll get into the Graph algebra stuff in the next post or two, but for now bear with me. Linear Difference models are very cool.

Linear difference models are basically recursively-calculated functions. They generally take the form:

*Y*(*t*+1) = *aY*(*t*) +* b*

Where *t* indicates a period in time, *t*+1 is the next period in time, and *a* and *b* are constants. This is strikingly similar to a much more familiar equation to anyone with high-school Algebra I. Does *Y* = *mx* + *b* ring any bells? It’s the same idea here. However, here’s the weird thing: this method draws curves.

Wait, What?

Yup, curves. Despite the name, we can use linear difference equations to generate the above graph (and many other cool ones). The idea is that because you’re recursively calculating the operative equation, the equation represents a straight line between two discrete points, *t* and *t*+1. And the line between the next two points will be different. Thus, when we literally connect the dots, we extrapolate a curve-like picture. (Those with a background in Calculus will recognize how this is simply an approximation, and that as the distance between the points is shrunk towards zero, a true curve will emerge. However, in order to do this, we’ll need to talk a little about the calculus-equivalent of Difference equations, which are of course Differential Equations–another topic for later).

Here’s how you do it in R.

a <- -.6 #slope b <- .3 #intercept timeserieslength <- 10 #Number of iterations y1 <- .3 #initial Value y2 <- 0 t <- 0 for (i in 1:timeserieslength) { y2[i] <- (a*y1[i])+b t[i] <- i if (i < timeserieslength){y1[i+1]=y2[i]}} plot(t, y2, type="o", ylab="Y", xlab="time", pch=20, lwd=2) title(main = list("Figure 1: Y over time", cex=1.5, col="black", font=1))

If you run the code as-is, you’ll notice something interesting: The code doesn’t match the graph shown above. In fact, this is a completely manufactured example, with no substantive basis. This is a by-product of the parameters you set the model to run with. The variables you can alter to play around with different graphs are pretty obvious: *a*, *b*, timeserieslength, and y1.

*a*is the same a as in the equation. It’s simply the slope. When we start using this technique to derive substantive information, we can parse this value out of a linear model of the data.*b*is also the same as is seen in the equation. It is the “y-intercept,” as every high-school algebra student knows. This can also be automatically extrapolated from a linear model.- timeserieslength is the number of values for which you want this model to run. If you’re measuring annual GDP over 30 years, this value should be 30. If you’re measuring weekly rainfall over a year, this should be 52. Easy, right?
- Y(1) is the initial condition. Frequently equal to the b value, but not always. Not equal to b when you want to start the calculation at a t value that is not zero.

So this is a good start. But let’s think about *a* a little bit. If *a* is negative, what happens? Well, if *a* > -1, you’ll have an interesting little oscillation that eventually hits an *equilibrium*. If *a* < -1, the oscillation increases as t increases, meaning you can follow *t* backwards to equilibrium, or forwards to increasing divergence. Now, the special case: what about *a* = -1? This one is cool. It bounces back and forth between two constant values. Very unusual, very useful. Here are some examples with their parameters:

A note about that *equilibrium* I just mentioned. We can calculate it deterministically! Check this out:

Equilibrium is the value Y* such that Y* = Y(t) = Y(t+1).

Substituting Y* into the first equation, Y* = aY* + b

Solving for Y* gives us Y* = b/(1-a)

Thus, for any linear difference equation (such that a ≠ 1), the limit as Y(t) approaches equilibrium is the Y* shown above.

Stay tuned. Next time I’ll show how to parse useful information out of a bivariate regression to make substantive models.

References:

Code Adapted from http://courtneybrown.com/classes/ModelingSocialPhenomena/Assignments/Assignment1CourtneyBrownMathModeling.htm

Brown, Courtney. “An Introduction to Linear Difference Equations.” Presentation for POLS 490, Emory University, Spring 2010. Available Online, http://courtneybrown.com/classes/ModelingSocialPhenomena/Presentations/DifferenceEquationsIntroduction.ppt

Huckfeldt, R. Robert, C. W. Kohfeld, and Thomas W. Likens. 1982. *Dynamic Modeling: An Introduction*. Newbury Park, California: Sage Publications. Series: *Quantitative Applications in the Social Sciences*, Number 27.

**leave a comment**for the author, please follow the link and comment on their blog:

**Nor Talk Too Wise » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...