-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeather.R
More file actions
49 lines (39 loc) · 1.51 KB
/
Copy pathWeather.R
File metadata and controls
49 lines (39 loc) · 1.51 KB
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
# read a csv of weather data
weather = read.csv("C:\\Users\\dlaut\\Documents\\Positron Projects\\nws_spokane.csv", as.is=T)
# calculate mean, deviation, standard deviation
mean_x <- mean(weather$AVGTEMP)
deviation <- weather$AVGTEMP - mean_x
# deviation
sd_x = sd(weather$AVGTEMP)
# sd_x
# histogram
hist(x = weather$AVGTEMP,
main = "Histogram of Avg Temp",
breaks=20, col="darkgreen", border="white")
# plot deviation
barplot(deviation, main = "Deviation from the Mean for Avg Temp", ylab = "Deviation", xlab = "Observation", col = ifelse(deviation > 0, "green", "blue"))
abline(h = 0, lwd = 2)
# plot standard deviation
plot(weather$AVGTEMP, pch = 19, main = "Avg Temp Values with ±1 SD Bands")
abline(h = mean_x, lwd = 2)
abline(h = mean_x + sd_x, col = "blue", lty = 2)
abline(h = mean_x - sd_x, col = "blue", lty = 2)
# plot temperature on x axis, index on y axis
plot(weather$AVGTEMP,
seq_along(weather$AVGTEMP),
pch = 19,
xlab = "Avg Temp",
ylab = "Observation",
main = "Avg Temp Values with ±1 SD Vertical Bands")
# draw shaded vertical band for ±1 SD
usr <- par("usr") # c(xmin, xmax, ymin, ymax)
rect(xleft = mean_x - sd_x,
ybottom = usr[3],
xright = mean_x + sd_x,
ytop = usr[4],
col = adjustcolor("blue", alpha.f = 0.15),
border = NA)
# add vertical lines for mean and ±1 SD
abline(v = mean_x, lwd = 2)
abline(v = mean_x + sd_x, col = "blue", lty = 2)
abline(v = mean_x - sd_x, col = "blue", lty = 2)