Quarto
library (tidyr)
library (broom)
library (lme4)
Loading required package: Matrix
Attaching package: 'Matrix'
The following objects are masked from 'package:tidyr':
expand, pack, unpack
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library (lme4)
library (lmerTest)
Attaching package: 'lmerTest'
The following object is masked from 'package:lme4':
lmer
The following object is masked from 'package:stats':
step
library (emmeans)
library (car)
Loading required package: carData
The following object is masked from 'package:dplyr':
recode
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ forcats 1.0.0 ✔ readr 2.1.5
✔ ggplot2 3.5.0 ✔ stringr 1.5.1
✔ lubridate 1.9.3 ✔ tibble 3.2.1
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ Matrix::expand() masks tidyr::expand()
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
✖ Matrix::pack() masks tidyr::pack()
✖ car::recode() masks dplyr::recode()
✖ purrr::some() masks car::some()
✖ Matrix::unpack() masks tidyr::unpack()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library (RColorBrewer)
library (ggplot2)
library (MASS)
Attaching package: 'MASS'
The following object is masked from 'package:dplyr':
select
library (agricolae)
library (vegan)
Loading required package: permute
Loading required package: lattice
This is vegan 2.6-4
library (dplyr)
library (readr)
library (DT)
library (ggplot2)
library (quantreg)
Loading required package: SparseM
Attaching package: 'SparseM'
The following object is masked from 'package:base':
backsolve
library (broom.mixed)
library (pedigree)
library (pedigreemm)
library (pedtools)
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org .
Reading data
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
rm (list = ls ())
setwd ("C: \\ Users \\ anunez \\ OneDrive - Iowa State University \\ Desktop \\ PIC_DataAnalysis_files" )
data_PIC <- read.csv ("FINAL_FIRE_2019_2023_AN.csv" )
summary (data_PIC)
ID LINE PED_IDENT_SIRE PED_IDENT_DAM
Min. : 80406526 Min. :65 Min. :72321895 Min. :70838758
1st Qu.: 84007778 1st Qu.:65 1st Qu.:79257130 1st Qu.:79153747
Median : 88690351 Median :65 Median :83589571 Median :84025461
Mean : 89253749 Mean :65 Mean :83724294 Mean :83988626
3rd Qu.: 93986447 3rd Qu.:65 3rd Qu.:87591160 3rd Qu.:88191389
Max. :100603759 Max. :65 Max. :95485445 Max. :96751344
LIT_LITTER_ID PEN TEST_FARM ENTRY_TIME
Min. :68967039 Length:1048575 Min. :774 Length:1048575
1st Qu.:71920248 Class :character 1st Qu.:774 Class :character
Median :74483118 Mode :character Median :774 Mode :character
Mean :74566106 Mean :774
3rd Qu.:77074566 3rd Qu.:774
Max. :80351636 Max. :774
EXIT_TIME STAY_IN FEED_INTK FEEDER_ENTRY_WT
Length:1048575 Min. : 2 Min. :-3892.0 Min. :-993
Class :character 1st Qu.: 513 1st Qu.: 231.0 1st Qu.: 786
Mode :character Median : 1103 Median : 521.0 Median :1051
Mean : 3582 Mean : 575.4 Mean :1106
3rd Qu.: 1757 3rd Qu.: 848.0 3rd Qu.:1399
Max. :665125948 Max. : 9280.0 Max. :9282
FEEDER_EXIT_WT FEEDER_NO START_DAY END_DAY
Min. :-993.0 Min. : 1.00 Length:1048575 Length:1048575
1st Qu.: 414.0 1st Qu.:14.00 Class :character Class :character
Median : 525.0 Median :31.00 Mode :character Mode :character
Mean : 530.8 Mean :30.19
3rd Qu.: 647.0 3rd Qu.:46.00
Max. :8182.0 Max. :66.00
data_PIC <- mutate (data_PIC,
ENTRY_DATE = as_date (mdy_hm (ENTRY_TIME, tz = "UTC" )),
ENTRY = mdy_hm (ENTRY_TIME, tz = "UTC" ),
EXIT_DATE = as_date (mdy_hm (EXIT_TIME, tz = "UTC" )),
EXIT = mdy_hm (EXIT_TIME, tz = "UTC" ),
START_DAY = format (as.Date (START_DAY, format = "%d-%b-%y" ), "%d%m%y" ),
OFFTEST_DAY = format (as.Date (END_DAY, format = "%d-%b-%y" ), "%d%m%y" ),
)
summary (data_PIC$ ENTRY_TIME)
Length Class Mode
1048575 character character
data_PIC$ PEN <- as.factor (data_PIC$ PEN)
data_PIC$ Social_Group <- paste (data_PIC$ PEN, data_PIC$ START_DAY, data_PIC$ OFFTEST_DAY, sep = "" )
head (data_PIC$ Social_Group)
[1] "B0315181023181223" "B0315181023181223" "B0315181023181223"
[4] "B0315181023181223" "B0315181023181223" "B0315181023181223"
data_PIC <- group_by (data_PIC, Social_Group)
Raw data arrange
data_PIC.arrange <- arrange (data_PIC, Social_Group, ENTRY, EXIT, by_group = TRUE )%>%
mutate (line = row_number ())
data_PIC.arrange <- data_PIC.arrange %>%
dplyr:: select (ID, ENTRY, EXIT, Social_Group)
head (data_PIC.arrange)
# A tibble: 6 × 4
# Groups: Social_Group [1]
ID ENTRY EXIT Social_Group
<int> <dttm> <dttm> <chr>
1 98782150 2023-05-24 05:40:00 2023-05-24 05:40:00 B0102240523240723
2 98782154 2023-05-24 05:48:00 2023-05-24 05:49:00 B0102240523240723
3 98782330 2023-05-24 05:58:00 2023-05-24 06:00:00 B0102240523240723
4 98782150 2023-05-24 06:03:00 2023-05-24 06:03:00 B0102240523240723
5 98753116 2023-05-24 06:05:00 2023-05-24 06:07:00 B0102240523240723
6 98782152 2023-05-24 07:13:00 2023-05-24 07:26:00 B0102240523240723
animal_counts1 <- data_PIC.arrange %>%
group_by (Social_Group) %>%
summarise (Unique_Animal_Count = n_distinct (ID))
animal_counts1
# A tibble: 309 × 2
Social_Group Unique_Animal_Count
<chr> <int>
1 B0102240523240723 16
2 B0104090519160719 15
3 B0104180719250919 15
4 B0104201218260219 14
5 B0104240523240723 16
6 B0104240621300821 14
7 B0104280219070519 15
8 B0106090519160719 2
9 B0106090720140920 16
10 B0106130220130420 16
# ℹ 299 more rows
social_group_distribution1 <- data_PIC.arrange %>%
group_by (Social_Group) %>%
summarise (Animal_Count = n ())
social_group_distribution1
# A tibble: 309 × 2
Social_Group Animal_Count
<chr> <int>
1 B0102240523240723 3541
2 B0104090519160719 4818
3 B0104180719250919 5113
4 B0104201218260219 3949
5 B0104240523240723 3290
6 B0104240621300821 2961
7 B0104280219070519 3988
8 B0106090519160719 632
9 B0106090720140920 4051
10 B0106130220130420 3488
# ℹ 299 more rows
write.csv (animal_counts1, "animal_counts_per_social_group1.csv" , row.names = FALSE )
write.csv (social_group_distribution1, "social_group_distribution1.csv" , row.names = FALSE )
social_group_data1 <- data_PIC.arrange %>%
group_by (Social_Group) %>%
summarise (Unique_Animal_Count = n_distinct (ID),
Animal_Count = n ())
social_group_data1
# A tibble: 309 × 3
Social_Group Unique_Animal_Count Animal_Count
<chr> <int> <int>
1 B0102240523240723 16 3541
2 B0104090519160719 15 4818
3 B0104180719250919 15 5113
4 B0104201218260219 14 3949
5 B0104240523240723 16 3290
6 B0104240621300821 14 2961
7 B0104280219070519 15 3988
8 B0106090519160719 2 632
9 B0106090720140920 16 4051
10 B0106130220130420 16 3488
# ℹ 299 more rows
write.csv (social_group_data1, "social_group_data1.csv" , row.names = FALSE )
#| warning: true
#| echo: true
animal_counts1 <- data_PIC.arrange %>%
group_by (Social_Group) %>%
summarise (Unique_Animal_Count = n_distinct (ID))
print (animal_counts1)
# A tibble: 309 × 2
Social_Group Unique_Animal_Count
<chr> <int>
1 B0102240523240723 16
2 B0104090519160719 15
3 B0104180719250919 15
4 B0104201218260219 14
5 B0104240523240723 16
6 B0104240621300821 14
7 B0104280219070519 15
8 B0106090519160719 2
9 B0106090720140920 16
10 B0106130220130420 16
# ℹ 299 more rows
social_group_distribution1 <- data_PIC.arrange %>%
group_by (Social_Group) %>%
summarise (Animal_Count = n ())
print (social_group_distribution1)
# A tibble: 309 × 2
Social_Group Animal_Count
<chr> <int>
1 B0102240523240723 3541
2 B0104090519160719 4818
3 B0104180719250919 5113
4 B0104201218260219 3949
5 B0104240523240723 3290
6 B0104240621300821 2961
7 B0104280219070519 3988
8 B0106090519160719 632
9 B0106090720140920 4051
10 B0106130220130420 3488
# ℹ 299 more rows
write.csv (animal_counts1, "animal_counts_per_social_group.csv" , row.names = FALSE )
write.csv (social_group_distribution1, "social_group_distribution.csv" , row.names = FALSE )
social_group_data1 <- data_PIC.arrange %>%
group_by (Social_Group) %>%
summarise (Unique_Animal_Count = n_distinct (ID),
Animal_Count = n ())
print (social_group_data1)
# A tibble: 309 × 3
Social_Group Unique_Animal_Count Animal_Count
<chr> <int> <int>
1 B0102240523240723 16 3541
2 B0104090519160719 15 4818
3 B0104180719250919 15 5113
4 B0104201218260219 14 3949
5 B0104240523240723 16 3290
6 B0104240621300821 14 2961
7 B0104280219070519 15 3988
8 B0106090519160719 2 632
9 B0106090720140920 16 4051
10 B0106130220130420 16 3488
# ℹ 299 more rows
Graphics
melted_data1 <- social_group_data1 %>%
pivot_longer (cols = c (Unique_Animal_Count, Animal_Count),
names_to = "Variable" ,
values_to = "Count" )
density1 <- ggplot (social_group_data1, aes (x = Animal_Count)) +
geom_density (fill = "skyblue" , alpha = 0.5 ) +
theme_minimal () +
labs (title = "Density Plot of Animal Counts per Social Group" ,
x = "Animal Count" ,
y = "Density" )
density1
histogram1 <- ggplot (social_group_data1, aes (x = Unique_Animal_Count, y = Animal_Count)) +
geom_bar (stat = "identity" , fill = "skyblue" ) +
theme_minimal () +
labs (title = "Distribution of Animal Counts per Social Group" ,
x = "Social Group" ,
y = "Animal Count" ) +
theme (axis.text.x = element_blank ())
histogram1
ggsave ("C: \\ Users \\ anunez \\ OneDrive - Iowa State University \\ Desktop \\ PIC_DataAnalysis_files \\ density1.png" , plot = density1, width = 10 , height = 6 , dpi = 300 )
ggsave ("C: \\ Users \\ anunez \\ OneDrive - Iowa State University \\ Desktop \\ PIC_DataAnalysis_files \\ histogram1.png" , plot = histogram1, width = 10 , height = 6 , dpi = 300 )
Creating variable time between feeders and separating records
You can add options to executable code like this
data_PIC <- data_PIC %>%
arrange (Social_Group, ENTRY) %>%
group_by (Social_Group) %>%
mutate (Follower_ID = lead (ID),
Follower_Time = lead (ENTRY),
Follower_Social_Group = lead (Social_Group),
line= row_number (),
Hour_ENTRY = hour (ENTRY),
time_between= as.numeric (Follower_Time - EXIT, unit= "secs" ))%>%
filter (time_between < 36000 ,time_between>= 0 )
data_PIC%>%
mutate (time_between= as.numeric (Follower_Time - ENTRY, unit= "secs" ),
lapse_Time = seconds (Follower_Time - ENTRY))%>%
dplyr:: select (time_between, lapse_Time)
Adding missing grouping variables: `Social_Group`
# A tibble: 1,038,897 × 3
# Groups: Social_Group [309]
Social_Group time_between lapse_Time
<chr> <dbl> <Period>
1 B0102240523240723 480 480S
2 B0102240523240723 600 600S
3 B0102240523240723 300 300S
4 B0102240523240723 120 120S
5 B0102240523240723 4080 4080S
6 B0102240523240723 900 900S
7 B0102240523240723 2280 2280S
8 B0102240523240723 240 240S
9 B0102240523240723 1020 1020S
10 B0102240523240723 300 300S
# ℹ 1,038,887 more rows
data_PIC$ time_between <- as.numeric (data_PIC$ time_between)
data_PIC_pvalues60 <- filter (data_PIC, time_between <= 60 ) %>%
mutate (TIME_FEEDER = as.numeric (STAY_IN))
summary (data_PIC_pvalues60$ TIME_FEEDER)
Min. 1st Qu. Median Mean 3rd Qu. Max.
4 556 1170 1266 1822 11206
# A tibble: 6 × 29
# Groups: Social_Group [1]
ID LINE PED_IDENT_SIRE PED_IDENT_DAM LIT_LITTER_ID PEN TEST_FARM
<int> <int> <int> <int> <int> <fct> <int>
1 98782187 65 93704219 94286020 79385516 B0102 774
2 98793248 65 93679672 91289790 79408788 B0102 774
3 98782187 65 93704219 94286020 79385516 B0102 774
4 98793431 65 93543632 91128767 79408787 B0102 774
5 98782150 65 93543534 94380138 79399025 B0102 774
6 98782150 65 93543534 94380138 79399025 B0102 774
# ℹ 22 more variables: ENTRY_TIME <chr>, EXIT_TIME <chr>, STAY_IN <int>,
# FEED_INTK <int>, FEEDER_ENTRY_WT <int>, FEEDER_EXIT_WT <int>,
# FEEDER_NO <int>, START_DAY <chr>, END_DAY <chr>, ENTRY_DATE <date>,
# ENTRY <dttm>, EXIT_DATE <date>, EXIT <dttm>, OFFTEST_DAY <chr>,
# Social_Group <chr>, Follower_ID <int>, Follower_Time <dttm>,
# Follower_Social_Group <chr>, line <int>, Hour_ENTRY <int>,
# time_between <dbl>, TIME_FEEDER <dbl>
Converting to Logarithm of time
data_PIC_pvalues60 <- data_PIC_pvalues60 %>%
mutate (L_time = log (TIME_FEEDER))
dim (data_PIC_pvalues60)
# A tibble: 6 × 30
# Groups: Social_Group [1]
ID LINE PED_IDENT_SIRE PED_IDENT_DAM LIT_LITTER_ID PEN TEST_FARM
<int> <int> <int> <int> <int> <fct> <int>
1 98782187 65 93704219 94286020 79385516 B0102 774
2 98793248 65 93679672 91289790 79408788 B0102 774
3 98782187 65 93704219 94286020 79385516 B0102 774
4 98793431 65 93543632 91128767 79408787 B0102 774
5 98782150 65 93543534 94380138 79399025 B0102 774
6 98782150 65 93543534 94380138 79399025 B0102 774
# ℹ 23 more variables: ENTRY_TIME <chr>, EXIT_TIME <chr>, STAY_IN <int>,
# FEED_INTK <int>, FEEDER_ENTRY_WT <int>, FEEDER_EXIT_WT <int>,
# FEEDER_NO <int>, START_DAY <chr>, END_DAY <chr>, ENTRY_DATE <date>,
# ENTRY <dttm>, EXIT_DATE <date>, EXIT <dttm>, OFFTEST_DAY <chr>,
# Social_Group <chr>, Follower_ID <int>, Follower_Time <dttm>,
# Follower_Social_Group <chr>, line <int>, Hour_ENTRY <int>,
# time_between <dbl>, TIME_FEEDER <dbl>, L_time <dbl>
Checking social groups with filter
# A tibble: 6 × 30
# Groups: Social_Group [1]
ID LINE PED_IDENT_SIRE PED_IDENT_DAM LIT_LITTER_ID PEN TEST_FARM
<int> <int> <int> <int> <int> <fct> <int>
1 98782187 65 93704219 94286020 79385516 B0102 774
2 98793248 65 93679672 91289790 79408788 B0102 774
3 98782187 65 93704219 94286020 79385516 B0102 774
4 98793431 65 93543632 91128767 79408787 B0102 774
5 98782150 65 93543534 94380138 79399025 B0102 774
6 98782150 65 93543534 94380138 79399025 B0102 774
# ℹ 23 more variables: ENTRY_TIME <chr>, EXIT_TIME <chr>, STAY_IN <int>,
# FEED_INTK <int>, FEEDER_ENTRY_WT <int>, FEEDER_EXIT_WT <int>,
# FEEDER_NO <int>, START_DAY <chr>, END_DAY <chr>, ENTRY_DATE <date>,
# ENTRY <dttm>, EXIT_DATE <date>, EXIT <dttm>, OFFTEST_DAY <chr>,
# Social_Group <chr>, Follower_ID <int>, Follower_Time <dttm>,
# Follower_Social_Group <chr>, line <int>, Hour_ENTRY <int>,
# time_between <dbl>, TIME_FEEDER <dbl>, L_time <dbl>
data_PIC.arrange1 <- arrange (data_PIC_pvalues60, Social_Group, ENTRY, EXIT, by_group = TRUE )%>%
mutate (line= row_number ())
data_PIC.arrange1 <- data_PIC.arrange1 %>%
dplyr:: select (ID, Follower_ID, ENTRY, EXIT, Social_Group)
print (data_PIC.arrange1)
# A tibble: 707,223 × 5
# Groups: Social_Group [308]
ID Follower_ID ENTRY EXIT Social_Group
<int> <int> <dttm> <dttm> <chr>
1 98782187 98791463 2023-05-24 08:36:00 2023-05-24 08:48:00 B01022405232407…
2 98793248 98782188 2023-05-24 09:51:00 2023-05-24 09:51:00 B01022405232407…
3 98782187 98793248 2023-05-24 11:33:00 2023-05-24 11:42:00 B01022405232407…
4 98793431 98782154 2023-05-24 13:26:00 2023-05-24 13:50:00 B01022405232407…
5 98782150 98793249 2023-05-24 13:59:00 2023-05-24 14:03:00 B01022405232407…
6 98782150 98782187 2023-05-24 14:07:00 2023-05-24 14:12:00 B01022405232407…
7 98782187 98791460 2023-05-24 14:13:00 2023-05-24 14:36:00 B01022405232407…
8 98791463 98782154 2023-05-24 15:56:00 2023-05-24 16:24:00 B01022405232407…
9 98782154 98791463 2023-05-24 16:24:00 2023-05-24 16:24:00 B01022405232407…
10 98791463 98782154 2023-05-24 16:24:00 2023-05-24 16:25:00 B01022405232407…
# ℹ 707,213 more rows
# A tibble: 6 × 5
# Groups: Social_Group [1]
ID Follower_ID ENTRY EXIT Social_Group
<int> <int> <dttm> <dttm> <chr>
1 98782187 98791463 2023-05-24 08:36:00 2023-05-24 08:48:00 B0102240523240723
2 98793248 98782188 2023-05-24 09:51:00 2023-05-24 09:51:00 B0102240523240723
3 98782187 98793248 2023-05-24 11:33:00 2023-05-24 11:42:00 B0102240523240723
4 98793431 98782154 2023-05-24 13:26:00 2023-05-24 13:50:00 B0102240523240723
5 98782150 98793249 2023-05-24 13:59:00 2023-05-24 14:03:00 B0102240523240723
6 98782150 98782187 2023-05-24 14:07:00 2023-05-24 14:12:00 B0102240523240723
animal_counts2 <- data_PIC.arrange1 %>%
group_by (Social_Group) %>%
summarise (Unique_Animal_Count2 = n_distinct (ID))
print (animal_counts2)
# A tibble: 308 × 2
Social_Group Unique_Animal_Count2
<chr> <int>
1 B0102240523240723 16
2 B0104090519160719 15
3 B0104180719250919 15
4 B0104201218260219 14
5 B0104240523240723 16
6 B0104240621300821 14
7 B0104280219070519 15
8 B0106090519160719 2
9 B0106090720140920 16
10 B0106130220130420 16
# ℹ 298 more rows
social_group_distribution <- data_PIC.arrange1 %>%
group_by (Social_Group) %>%
summarise (Animal_Count2 = n ())
print (social_group_distribution)
# A tibble: 308 × 2
Social_Group Animal_Count2
<chr> <int>
1 B0102240523240723 2775
2 B0104090519160719 3303
3 B0104180719250919 3010
4 B0104201218260219 2527
5 B0104240523240723 2164
6 B0104240621300821 1851
7 B0104280219070519 2513
8 B0106090519160719 60
9 B0106090720140920 2725
10 B0106130220130420 2753
# ℹ 298 more rows
write.csv (animal_counts2, "animal_counts_per_social_group.csv" , row.names = FALSE )
write.csv (social_group_distribution, "social_group_distribution.csv" , row.names = FALSE )
social_group_data <- data_PIC.arrange1 %>%
group_by (Social_Group) %>%
summarise (Unique_Animal_Count2 = n_distinct (ID),
Animal_Count2 = n ())
print (social_group_data)
# A tibble: 308 × 3
Social_Group Unique_Animal_Count2 Animal_Count2
<chr> <int> <int>
1 B0102240523240723 16 2775
2 B0104090519160719 15 3303
3 B0104180719250919 15 3010
4 B0104201218260219 14 2527
5 B0104240523240723 16 2164
6 B0104240621300821 14 1851
7 B0104280219070519 15 2513
8 B0106090519160719 2 60
9 B0106090720140920 16 2725
10 B0106130220130420 16 2753
# ℹ 298 more rows
write.csv (social_group_data, "social_group_data.csv" , row.names = FALSE )
Graphics distribution
melted_data <- social_group_data %>%
pivot_longer (cols = c (Unique_Animal_Count2, Animal_Count2),
names_to = "Variable" ,
values_to = "Count" )
density <- ggplot (social_group_data, aes (x = Animal_Count2)) +
geom_density (fill = "skyblue" , alpha = 0.5 ) +
theme_minimal () +
labs (title = "Density Plot of Animal Counts per Social Group" ,
x = "Animal Count" ,
y = "Density" )
density
histogram <- ggplot (social_group_data, aes (x = Unique_Animal_Count2, y = Animal_Count2)) +
geom_bar (stat = "identity" , fill = "skyblue" ) +
theme_minimal () +
labs (title = "Distribution of Animal Counts per Social Group" ,
x = "Social Group" ,
y = "Animal Count" ) +
theme (axis.text.x = element_blank ())
histogram
ggsave ("C: \\ Users \\ anunez \\ OneDrive - Iowa State University \\ Desktop \\ PIC_DataAnalysis_files \\ density.png" , plot = density, width = 10 , height = 6 , dpi = 300 )
ggsave ("C: \\ Users \\ anunez \\ OneDrive - Iowa State University \\ Desktop \\ PIC_DataAnalysis_files \\ histogram.png" , plot = histogram, width = 10 , height = 6 , dpi = 300 )
write.csv (data_PIC.arrange1, "Social_Network_PIC.csv" , row.names = FALSE )
The echo: false option disables the printing of code (only output is displayed).