1 #!/usr/local/bin/R
 2 
 3 # Xavier Fernández i Marín
 4 # December 2004
 5 # http://xavier-fim.net
 6 
 7 
 8 # Basic regression
 9 
10 # Simulate data 
11 sim <- 500             # number of observations
12 var1 <- rnorm(500)
13 var2 <- rnorm(500)
14 var3 <- rnorm(500)
15 
16 # Simulate a process that links the data with some other variable
17 # and add some random noise
18 dependent <- rep(1, sim) + 0.5*var1 - 3*var2 + 2*var3 + rnorm(sim)
19 
20 
21 # Link the four vectors into a unique dataframe
22 df <- data.frame(dependent, var1, var2, var3)
23 pairs(df)
24 
25 
26 # basic model specification
27 basic <- lm(dependent ~ var1 + var2 + var3)
28 
29 # Inspect the output generated by the linear model object
30 basic
31 summary(basic)
32 attributes(basic)
33 
34 # model search
35 anova(basic)
36 step(basic)
37 
38 
39 # Complex model specification
40 complex1 <- lm(dependent ~ var1 * var2)
41            # This includes var1, var2 and the interaction between them
42 
43 complex2 <- lm(dependent ~ var1 + var1:var2)
44            # This includes var1 and the interaction between var1 and var2
45 
46 complex3 <- lm(dependent ~ log(var1) + var2 * var3)
47            # Includes var1 logged, var2, var3 and interaction var2 and var3
48 
49 complex4 <- lm(dependent ~ var1 + var2 + I(var2)^2)
50            # Includes var1, var2 and square of var2
51 
52 # Plot regression models
53 plot(basic)
54             # will ask to hit <return> to see each plot in the same window
55 
56 # we can plot the four plots in the same window
57 par(mfrow=c(2,2))
58 plot(basic)
59             # It is really easy to do a basic inspection of residuals
60 
61 
62 # Partial plots of the residuals 
63 plot(var1, basic$residuals)
64 plot(var2, basic$residuals)
65 plot(var3, basic$residuals)
66 
67 
68