-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature_correlation.R
More file actions
134 lines (109 loc) · 2.67 KB
/
temperature_correlation.R
File metadata and controls
134 lines (109 loc) · 2.67 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
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
### DESCRIPTION ###############################################################
# Assess correlation between temperature and food consumption
#
#
### LIBRARIES #################################################################
library(CatterPlots);
library(hedgehog);
library(dplyr);
options(stringsAsFactors = FALSE);
rm(list = ls(all.names = TRUE));
source.directory('helper-functions');
### MAIN ######################################################################
temperatures <- read.csv('data/toronto_temperatures.csv');
# restrict to dates we have complete data
temperatures <- temperatures %>%
select(Date.Time, Max.Temp..Â.C., Min.Temp..Â.C., Mean.Temp..Â.C.) %>%
mutate_at('Date.Time', as.Date) %>%
filter(Date.Time >= as.Date('2017-04-28') & Date.Time <= as.Date('2017-07-07'));
names(temperatures) <- c('date', 'max_temp', 'min_temp', 'mean_temp');
# add food consumption data
food.by.date <- get.consumption.by.day('total');
temperatures <- merge(temperatures, food.by.date);
options(bitmapType = 'cairo');
png(
'plots/temperature_panel.png',
width = 8,
height = 5,
units = 'in',
res = 500
);
par(mar = c(1, 4, 0.5, 0.1), mfrow = c(2, 1) );
plot(
temperatures$mean_temp,
type = 'l',
bty = 'n',
xaxt = 'n',
xlab = '',
ylab = 'Mean temperature (°C)',
ylim = c(0, 27),
lwd = 2,
xaxs = 'i'
);
barplot(
temperatures$consumption,
ylab = 'Food consumed (g)',
xaxs = 'i',
col = 'khaki3'
);
mtext(
'May',
side = 1,
at = 1.2*which(as.Date('2017-05-15') == temperatures$date)
);
mtext(
'June',
side = 1,
at = 1.2*which(as.Date('2017-06-15') == temperatures$date)
);
dev.off();
png(
'plots/temperature_correlation.png',
width = 6,
height = 5,
units = 'in',
res = 500
);
par(mar = c(4, 4, 0.1, 0.1));
purr <- multicat(
temperatures$mean_temp,
temperatures$consumption,
catcolor = 'maroon',
size = 0.05,
xlim = c(0, 27),
xat = seq(0, 27, by = 5),
xlab = 'Mean temperature (°C)',
ylim = c(0, 380),
yat = seq(0, 400, by = 100),
ylab = 'Food consumed (g)',
cat = sample(
1:10,
nrow(temperatures),
replace = TRUE
),
canvas = c(0, 1.04, 0, 1.0),
bty = 'L'
);
correlation.test <- cor.test(
x = temperatures$mean_temp,
y = temperatures$consumption,
method = 'spearman'
);
text(
labels = bquote( rho == .(round(correlation.test$estimate, 3) ) ),
x = 0.03,
y = 0.1,
adj = c(0, 0.5)
);
text(
labels = bquote( P == .(scientific.notation(correlation.test$p.value) ) ),
x = 0.03,
y = 0.05,
adj = c(0, 0.5)
);
dev.off();
lm.model <- lm(
consumption ~ mean_temp,
temperatures
);
print( summary(lm.model) );