我正在分析在规定时间段内的几种动物行为。
我观看动物及其行为的视频。我记录每个行为的显示时间。他们将在录制期间多次显示每个行为(对应于不同的事件)。有时在录制过程中可以同时显示2或3个行为,但它们通常不会在同一时间完全开始/结束(因此它们部分重叠)。
我最终得到了每个行为的一系列事件,对于每个事件,我都有它们的起始点,持续时间和终点(见下文中的示例)。
我需要从这个数据中提取行为1与行为2重叠的总量/行为1与行为3重叠/行为2与行为3重叠。这样我就可以找到行为之间的相关性同时显示,哪些不显示,......
我只是编程的初学者(主要是R),我发现很难开始。你能告诉我怎么办吗?非常感谢!
针对3种行为的一系列事件的示例:
Event tracked Onset Duration End
Behaviour 1 _event 1 7.40 548.88 556.28
Behaviour 1 _event 2 36.20 0.47 36.67
Behaviour 1 _event 3 48.45 0.25 48.70
Behaviour 1 _event 4 68.92 1.53 70.45
Behaviour 1 _event 5 75.48 0.22 75.70
Behaviour 1 _event 6 89.75 0.66 90.41
Behaviour 1 _event 7 94.62 0.16 94.78
Behaviour 1 _event 8 101.78 0.22 102.00
Behaviour 1 _event 9 108.86 0.59 109.45
Behaviour 1 _event 10 146.35 0.66 147.00
Behaviour 1 _event 11 150.20 0.75 150.95
Behaviour 1 _event 12 152.98 0.66 153.64
Behaviour 1 _event 13 157.84 0.56 158.41
Behaviour 2_event 1 7.52 0.38 7.90
Behaviour 2_event 2 18.73 0.16 18.88
Behaviour 2_event 3 19.95 2.25 22.20
Behaviour 2_event 4 26.41 0.25 26.66
Behaviour 2_event 5 35.91 0.16 36.07
Behaviour 2_event 6 37.29 0.34 37.63
Behaviour 2_event 7 38.13 0.72 38.85
Behaviour 2_event 8 40.19 0.31 40.51
Behaviour 2_event 9 44.26 0.16 44.41
Behaviour 2_event 10 45.32 0.16 45.48
Behaviour 2_event 11 54.84 1.44 56.27
Behaviour 2_event 12 56.65 1.19 57.84
Behaviour 2_event 13 61.59 1.03 62.62
Behaviour 2_event 14 81.13 3.83 84.96
Behaviour 2_event 15 86.65 0.31 86.96
Behaviour 2_event 16 90.15 0.19 90.34
Behaviour 2_event 17 96.97 0.53 97.50
Behaviour 2_event 18 107.12 0.22 107.34
Behaviour 2_event 19 118.53 0.41 118.94
Behaviour 2_event 20 127.76 0.25 128.01
Behaviour 2_event 21 129.45 0.69 130.13
Behaviour 2_event 22 130.60 2.31 132.91
Behaviour 2_event 23 141.01 0.41 141.41
Behaviour 2_event 24 152.85 0.37 153.23
Behaviour 2_event 25 156.54 0.13 156.66
Behaviour 3_event 1 7.71 1.94 9.65
Behaviour 3_event 2 11.12 1.53 12.65
Behaviour 3_event 3 19.01 0.19 19.20
Behaviour 3_event 4 20.01 3.97 23.98
Behaviour 3_event 5 24.95 4.22 29.16
Behaviour 3_event 6 29.70 2.19 31.88
Behaviour 3_event 7 33.23 2.50 35.73
Behaviour 3_event 8 36.82 0.44 37.26
Behaviour 3_event 9 38.20 1.16 39.35
Behaviour 3_event 10 39.91 2.13 42.04
Behaviour 3_event 11 42.49 3.62 46.11
Behaviour 3_event 12 47.09 0.53 47.62
Behaviour 3_event 13 48.15 0.34 48.49
Behaviour 3_event 14 49.40 2.13 51.52
Behaviour 3_event 15 57.57 2.25 59.82
Behaviour 3_event 16 60.89 0.88 61.76
Behaviour 3_event 17 66.85 6.78 73.63
Behaviour 3_event 18 75.65 3.03 78.68
答案 0 :(得分:0)
为了进行您想要进行的研究,最简单的方法是将数据转换为状态变量的时间序列(即是否显示行为1,2,3等)。所以您想要将您拥有的数据集转换为类似
的数据集time animal behav_1 behav_2 behav_3
0 1 FALSE TRUE FALSE
0 2 TRUE FALSE FALSE
1 1 FALSE TRUE FALSE
1 2 TRUE FALSE TRUE
... ... ... ... ...
每行告诉特定动物是否在给定时间显示三种行为中的每一种。 (我假设你有多个动物,你想保持他们的行为数据分开。)
然后您可以轻松地估计您感兴趣的许多数量。例如,您可以计算动物行为的概率1,因为它正在做行为2
data$behav_1_and_2 <- data$behav_1 & data$behav_2
behav_1_and_2
的总和除以behav_2
的总和:sum(data$behav_1_and_2) / sum(data$behav_2)
好的,但是你如何改造数据呢?首先,确定要检查的时间点数。也许你应该增加约0.1。
num_animals <- 10 // How many animals you have
time_seq <- seq(from = 0, to = 600, by = 0.1) // to should be end of video
data <- expand.grid(time = time_seq, animal = num_animals)
它可以获得所需数据框的前两列。然后,您需要计算三个行为列。定义一个函数,该函数接受行为列的time
,animal
和名称,如果动物当时正在执行该行为,则返回TRUE
,如果FALSE
,则返回has_behavior <- function(time, animal, behavior) {
...
}
不
// First create empty columns
data$behav_1 <- logical(nrow(data))
data$behav_2 <- logical(nrow(data))
data$behav_3 <- logical(nrow(data))
// Now loop through rows
for (i in 1:nrow(data)) {
data$behav_1[i] <- has_behavior(data$time[i], data$animal[i], 1)
data$behav_2[i] <- has_behavior(data$time[i], data$animal[i], 2)
data$behav_3[i] <- has_behavior(data$time[i], data$animal[i], 3)
}
(我将让你弄明白如何制作这个功能。)手头有了这个功能,你可以用循环创建最后三列:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<!-- CSS Reset -->
<style>
/* What it does: Remove spaces around the email design added by some email clients. */
/* Beware: It can remove the padding / margin and add a background color to the compose a reply window. */
html,
body {
margin: 0 auto !important;
padding: 0 !important;
height: 100% !important;
width: 100% !important;
}
/* What it does: Stops email clients resizing small text. */
* {
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
/* What is does: Centers email on Android 4.4 */
div[style*="margin: 16px 0"] {
margin:0 !important;
}
/* What it does: Stops Outlook from adding extra spacing to tables. */
table,
td {
mso-table-lspace: 0pt !important;
mso-table-rspace: 0pt !important;
overflow-wrap: break-word;
word-wrap: break-word; }
/* What it does: Fixes webkit padding issue. Fix for Yahoo mail table alignment bug. Applies table-layout to the first 2 tables then removes for anything nested deeper. */
table {
border-spacing: 0 !important;
border-collapse: collapse !important;
table-layout: fixed !important;
margin: 0 auto !important;
}
.counter {
font-size: 40px;
line-height: 40px;
font-weight: 400;
color: #73879C;
border-left: 2pxsolid#ADB2B5;
float: left;
}
.subcounter {
font-size: 10px;font-weight: 300;
border-left: 2pxsolid#ADB2B5;
float: left;
}
}
</style>
<table role="presentation" cellspacing="0" cellpadding="0" align="left" width="100%" style="">
<tr>
<td style="padding: 20px 0; text-align: center; height="75" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<br><br><div class="counter">99,999,999</div>
<table role="presentation" cellspacing="0" cellpadding="0" align="left" width="100%" style="">
<tr>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% WoW
</td>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% MoM
</td>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% YoY
</td>
</td>
</table>
<td style="padding: 20px 0; text-align: center; height="50" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<br><br><div class="counter">99,999,999</div>
<table role="presentation" cellspacing="0" cellpadding="0" align="left" width="100%" style="">
<tr>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% WoW
</td>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% MoM
</td>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% YoY
</td>
</td>
</table>
</td>
<td style="padding: 20px 0; text-align: center; height="50" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<br><br><div class="counter">99,999,999</div>
<table role="presentation" cellspacing="0" cellpadding="0" align="left" width="100%" style="">
<tr>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% WoW
</td>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% MoM
</td>
<td style="padding: 5px 0; text-align: center; height="25" alt="alt_text" border="0" bgcolor="#dddddd" color: "#555555">
<div class="subcounter">⌄ 10% YoY
</td>
</td>
</table>
</td>
</tr>
</table>
</body></html>
使用此格式的数据,您应该能够更轻松地研究问题。您可以轻松地计算这些汇总数量,如前所述。该数据框也被设置为对进行时间序列建模有用。它也是tidy,因此可以轻松地使用dplyr等数据包进行数据汇总,并ggplot2进行可视化。 (您可以在免费在线书籍R for Data Science by Hadley Wickham中了解有关最后两种工具的更多信息。)