-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity.r
147 lines (127 loc) · 3.4 KB
/
activity.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
143
144
145
146
147
# TODO
# write a vignette
# subsample to 1h and draw diel activities boxplots
# persp x,y and pythagaros
c('cheetah1', 'leopard') %>%
mapply(
n = c('c1', 'l'),
SIMPLIFY = F,
FUN = function(x, n){
x %>%
paste0('extdata/move/', .,
'-activity.txt') %>%
fread(header = TRUE,
sep = ' ') %>%
cbind(iid = n) -> x
}) -> tb
tb %<>% do.call(rbind,.)
# temp: degree celcius
# activity: no units
# strptime first creates posixlt
tb <- tb[,{
time <- strptime(paste(UTCDate,UTCTime),
format = "%d.%m.%Y %H:%M:%S",
tz="UTC") %>%
round(units="mins") %>%
as.POSIXct
activity <- sqrt(ActivityX^2 + ActivityY^2)
doy <- strftime(time,format = "%j",tz="UTC") %>% as.numeric # day of year. tz is mandatory or they are treated as local tz
ydoy <- paste(strftime(time, format = "%Y", tz="UTC"),doy,sep='-') # year and day of year
x <- strftime(time,format="%H", tz="UTC") %>% as.numeric
tod <- ifelse(x >= 0 & x < 8, 0,ifelse(x >= 8 & x < 16, 8, 16)) # time of day
ydoy_tod <- paste(ydoy,tod,sep='-')
# rm(x,doy)
c(.SD, # .SD is a datatable
.(
time = time,
activity=activity,
ydoy=ydoy,
tod=tod,
ydoy_tod=ydoy_tod
))
}
]
tb <- tb[(time >= min(move$time))]
yy <- tb[,
.(act_mean_diel=mean(activity),
time = min(time)
),
by=.(iid, ydoy)
][act_mean_diel > 1]
# scatterplot(act_mean_diel ~ time,
# yy,
# group = factor(yy$iid),
# legend=TRUE,
# # type='l', # doesn't work. because plot(type='n',...) is called
# regLine=FALSE,
# boxplot=FALSE,
# smooth=list(span=.08) # doesn't work with PoSIXct
# )
# png_inch('activity%03d.png',
# width = 14,
# height = 7)
# dev.new(width=14, height=7)
layout(matrix(1))
my_par(mar=c(4,5,1,0))
yy %$% {
plot(
time,
act_mean_diel,
type = 'n',
col = 'red',
xlab = 'Time of Year [2 months]',
ylab = expression('Net activity ['*degree*']:'*sqrt(X[activity]^2 + Y[activity]^2)),
main = paste0('Mean Diel Net Activity')
)
}
yy %$%
split(., iid) %>%
mapply(1:length(.),FUN=function(x, i)
x %$% {
points(time,
act_mean_diel,
pch = i,
col = i,
cex= .5
)
lines(time,act_mean_diel,
col = i)
lines(loess.smooth(time,
act_mean_diel,
span = .08),
lwd=2,
col = i + 2)
(a <- lm(act_mean_diel~time, .)) %>% # l is significant
summary %>%
print
abline(a,
lwd=2,
col = i + 4)
})
legend("topright",
# rep(c("C1","L"), 3),
paste(
rep(c("C1","L"),3),
rep(c("timeseries",
"local polynomial",
"linear regression"),
each=2)),
lwd=2,
col=1:6)
# dev.off()
# parse behavioral modes
tb[,
.(act_mean = mean(activity),
act_sd = sd(activity),
time = min(time)
),
by=.(iid, ydoy_tod)][act_mean > 1] -> x
move <- as.data.table(move)
xy <- move[x,on=.(iid,time), nomatch=0]
# png_inch('parse-behavior.png')
my_par(cex.axis=1)
layout(matrix(1))
boxplot(act_mean~iid*mode_null,
xy,
ylab='Net activity')
# dev.off()