-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmustang.R
142 lines (124 loc) · 3.85 KB
/
mustang.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
library(ggplot2)
m1<-read.table("mustangOLSdata.txt", header=TRUE, sep='\t')
lmfit_univar <- lm(price~years,m1)
# summary(lmfit_univar)
#
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 34129.6 1517.1 22.497 1.55e-13 ***
#years -2832.8 366.7 -7.724 8.72e-07 ***
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 2622 on 16 degrees of freedom
#Multiple R-squared: 0.7885, Adjusted R-squared: 0.7753
lmfit <- lm(price~years + convertible,m1)
# summary(lmfit)
#
#Coefficients:
# Estimate Std. Error t value Pr(>|t|)
#(Intercept) 32253.8 1058.8 30.463 6.63e-15 ***
#years -2698.8 239.5 -11.268 1.02e-08 ***
#convertible 4108.8 856.2 4.799 0.000234 ***
#---
#Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#
#Residual standard error: 1701 on 15 degrees of freedom
#Multiple R-squared: 0.9166, Adjusted R-squared: 0.9055
#F-statistic: 82.43 on 2 and 15 DF, p-value: 8.107e-09
convertibles <-subset(m1[ which(m1$convertible==1),])
attach(m1)
png(filename="mustang1.png",
width=5, height=4, units="in",
pointsize=12, bg="white", res=300,
type="quartz"
)
ggplot(data=m1, aes(x=years,y=price)) +
geom_point() +
labs(title="Price of Mustangs Versus Their Age",
x="Age, in Years",
y="Sale Price"
)
dev.off()
png(filename="mustang2.png",
width=5, height=4, units="in",
pointsize=12, bg="white", res=300,
type="quartz"
)
ggplot(data=m1, aes(x=years,y=price)) +
geom_point() +
labs(title="Price of Mustangs Versus Their Age",
x="Age, in Years",
y="Sale Price"
) +
geom_abline(data=m1,
intercept=lmfit_univar$coefficients[1],
slope=lmfit_univar$coefficients[2],
color="brown"
)
dev.off()
png(filename="mustang3.png",
width=5, height=4, units="in",
pointsize=12, bg="white", res=300,
type="quartz"
)
ggplot(data=m1, aes(x=years,y=price)) +
geom_point() +
labs(title="Price of Mustangs Versus Their Age",
x="Age, in Years",
y="Sale Price") +
geom_point(color="red",
size=3,
data=convertibles
)
dev.off()
png(filename="mustang4.png",
width=5, height=4, units="in",
pointsize=12, bg="white", res=300,
type="quartz"
)
ggplot(data=m1, aes(x=years,y=price)) +
geom_point() +
labs(title="Price of Mustangs Versus Their Age",
x="Age, in Years",
y="Sale Price"
) +
geom_point(color="red",
size=3,
data=convertibles
) +
geom_abline(data=m1,
intercept=lmfit$coefficients[1],
slope=lmfit$coefficients[2],
color="blue",
size=1
) +
geom_abline(data=m1,
intercept=lmfit_univar$coefficients[1],
slope=lmfit_univar$coefficients[2],
color="gray"
)
dev.off()
# How to extend the x & y axes ranges here?
#png(filename="mustang5.png", width=5, height=4, units="in", pointsize=12, bg="white", res=300, type="quartz")
ggplot(data=m1, aes(x=years,y=price )) +
coord_cartesian(xlim = c(0, 10)) +
coord_cartesian(ylim = c(0, 40000)) +
geom_point() +
labs(title="Price of Mustangs Versus Their Age",
x="Age, in Years",
y="Sale Price") +
geom_point(color="red", size=3, data=convertibles) +
geom_abline(data=m1,
intercept=lmfit$coefficients[1],
slope=lmfit$coefficients[2],
color="blue",
size=1
) +
geom_abline(data=m1,
intercept=lmfit_univar$coefficients[1],
slope=lmfit_univar$coefficients[2],
color="gray"
)
#dev.off()
detach(m1)