Plotting Data - Introduction

Two types of data that we work with:

  1. Functional
  2. Time series

Visual inspection is always our first approach to analyzing either type of data set.

In [175]:
## Choose our domain (1,10] at 0.1 resolution.
x = seq(1,10,0.1)
l.x = length(x)

## Start with these four
y1 = 2*(x)-0.2+rnorm(l.x,0,1)
y2 = 2^(x+rnorm(l.x,0,1))
y3 = (x+rnorm(l.x,0,1))^2.12+22
y4 = 2/(x+2)+rnorm(l.x,0,0.1)
In [176]:
par(mfrow=c(2,2))
plot(x=x, y=y1, main='y1')
plot(x=x, y=y2, main='y2')
plot(x=x, y=y3, main='y3')
plot(x=x, y=y4, main='y4')
In [177]:
## Linear fit models
y1.lm = lm(y1~x)
y2.lm = lm(y2~x)
y3.lm = lm(y3~x)
y4.lm = lm(y4~x)

## Plot the data and linear models
par(mfrow=c(2,2))
plot(x=x, y=y1, main=paste('y1 - r^2:',summary(y1.lm)$adj.r.squared))
lines(x=x, y=predict(y1.lm), col='red', lwd=2)

plot(x=x, y=y2, main=paste('y2 - r^2:',summary(y2.lm)$adj.r.squared))
lines(x=x, y=predict(y2.lm), col='red', lwd=2)

plot(x=x, y=y3, main=paste('y3 - r^2:',summary(y3.lm)$adj.r.squared))
lines(x=x, y=predict(y3.lm, data.frame(x)), col='red', lwd=2)

plot(x=x, y=y4, main=paste('y4 - r^2:',summary(y4.lm)$adj.r.squared))
lines(x=x, y=predict(y4.lm, data.frame(x)), col='red', lwd=2)
In [178]:
## Look at the options for y2 (y=2^x)
par(mfrow=c(2,2))
plot(x=x,y=y2, main='original')
plot(x=log(x),y=log(y2), main='log-log')
plot(x=log(x),y=y2, main='log(x)')
plot(x=(x),y=log(y2), main='log(y)')
In [184]:
## Make our models again but with the transforms
y2.lx = lm(y2~log(x+0.001))
y2.ly = lm(log(y2+0.001)~x)
y2.ll = lm(log(y2+0.001)~log(x+0.001))

par(mfrow=c(2,2))
plot(x=x, y=y2, main=paste('y2 - r^2:',summary(y2.lm)$adj.r.squared))
lines(x=x, y=predict(y2.lm), col='red', lwd=2)

plot(x=log(x), y=y2, main=paste('log x - r^2:',summary(y2.lx)$adj.r.squared))
lines(x=log(x), y=predict(y2.lx), col='red', lwd=2)

plot(x=x, y=log(y2), main=paste('log y - r^2:',summary(y2.ly)$adj.r.squared))
lines(x=x, y=predict(y2.ly), col='red', lwd=2)

plot(x=log(x), y=log(y2), main=paste('log log - r^2:',summary(y2.ll)$adj.r.squared))
lines(x=log(x), y=predict(y2.ll), col='red', lwd=2)

summary(y2.ll)
Out[184]:
11.3560205386009
In [180]:
y3.lx = lm(y3~log(x))
y3.ly = lm(log(y3)~x)
y3.ll = lm(log(y3)~log(x))

par(mfrow=c(2,2))
plot(x=x, y=y3, main=paste('y3 - r^2:',summary(y3.lm)$adj.r.squared))
lines(x=x, y=predict(y3.lm, data.frame(x)), col='red', lwd=2)

plot(x=log(x), y=y3, main=paste('log x - r^2:',summary(y3.lx)$adj.r.squared))
lines(x=log(x), y=predict(y3.lx, data.frame(x)), col='red', lwd=2)

plot(x=x, y=log(y3), main=paste('log y - r^2:',summary(y3.ly)$adj.r.squared))
lines(x=x, y=predict(y3.ly, data.frame(x)), col='red', lwd=2)

plot(x=log(x), y=log(y3), main=paste('log log - r^2:',summary(y3.ll)$adj.r.squared))
lines(x=log(x), y=predict(y3.ll, data.frame(x)), col='red', lwd=2)