Short script to show how to transform dependent and independent variable(s) to model non-linear relationships using regression models.

Basic Models: Linear, Power, Exponential

Data

x<-c(0:8)
y<-c(17,23,29,35,43,52,63,77,87)
back to top

Linar Model

Linear.Model <- lm(y~x)
summary(Linear.Model)
## 
## Call:
## lm(formula = y ~ x)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.3333 -3.5500 -0.7667  3.3167  4.8000 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  12.2000     2.4496    4.98   0.0016 ** 
## x             8.7833     0.5145   17.07 5.81e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.985 on 7 degrees of freedom
## Multiple R-squared:  0.9765, Adjusted R-squared:  0.9732 
## F-statistic: 291.4 on 1 and 7 DF,  p-value: 5.806e-07

Transformations

LN.x<-log(x)
y17<-y-17
LN.y<-log(y)
LN.y17<-log(y-17)
back to top

Power Model

Power.Model<-lm(LN.y17[2:9]~LN.x[2:9])
summary(Power.Model)
## 
## Call:
## lm(formula = LN.y17[2:9] ~ LN.x[2:9])
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.10785 -0.06007 -0.01305  0.08139  0.10423 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  1.68753    0.07019   24.04  3.4e-07 ***
## LN.x[2:9]    1.19304    0.04743   25.15  2.6e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.08827 on 6 degrees of freedom
## Multiple R-squared:  0.9906, Adjusted R-squared:  0.989 
## F-statistic: 632.7 on 1 and 6 DF,  p-value: 2.6e-07

Compute the constant “a” for the power model

a.power<-exp(Power.Model$coeff[1])    ##exp(x) is the inverse of log
back to top

Exponential Model

Exponential.Model<-lm(LN.y~x)
summary(Exponential.Model)
## 
## Call:
## lm(formula = LN.y ~ x)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.08842  0.01013  0.01214  0.02855  0.04222 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.921630   0.030405   96.09 3.48e-12 ***
## x           0.201721   0.006386   31.59 8.24e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.04947 on 7 degrees of freedom
## Multiple R-squared:  0.993,  Adjusted R-squared:  0.992 
## F-statistic: 997.7 on 1 and 7 DF,  p-value: 8.237e-09

Compute the constant “a” for the exponential model

a.exponential<-exp(Exponential.Model$coeff[1]) 
back to top

In this section, the future values in each model are computed so they can be plotted

Projection (out of sample)

Future.x<-c(x,9,10,11)

## Linear
Future.y.linear<-Linear.Model$coeff[1]+Future.x*Linear.Model$coeff[2]

## Power
Future.y.power<-a.power*(Future.x^Power.Model$coeff[2])+y[1]

## Exponential
Future.y.exponential<-a.exponential*exp(Exponential.Model$coeff[2]*Future.x)
back to top

Plotting the predictive models using line charts

back to top