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
# Arrange the data and add a row numberdata_PIC_arranged <- data_PIC_pvalues60 %>%arrange(Social_Group, ENTRY, EXIT, by_group =TRUE) %>%mutate(line =row_number())dim(data_PIC_arranged)
[1] 707223 30
# Filter the data and select specific columnsdata_PIC_filtered <- data_PIC_pvalues60 %>%filter(ID != Follower_ID) %>% dplyr::select(ID, Follower_ID, ENTRY, EXIT, STAY_IN, Social_Group, L_time, Hour_ENTRY, time_between, FEED_INTK, PEN)data_PIC_pvalues60
unique_ids_per_group <- data_PIC_filtered %>%group_by(Social_Group) %>%summarise(Unique_IDs =n_distinct(ID),.groups ='drop') %>%# Calculate the mean of Unique_IDs across all groupssummarise(Mean_Unique_IDs =mean(Unique_IDs))# View the resultsprint(unique_ids_per_group)
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: reciprocal condition number 1.0962e-15
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: There are other near singularities as well. 1
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
16.07
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
2.07
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
number 1.0962e-15
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : There are other near
singularities as well. 1
# Arrange the data and add a row numberdata_PIC_arranged1 <- data_PIC_pvalues60_distant %>%arrange(Social_Group, ENTRY, EXIT, by_group =TRUE) %>%mutate(line =row_number())dim(data_PIC_arranged1)
[1] 331674 30
# Filter the data and select specific columnsdata_PIC_filtered1 <- data_PIC_pvalues60_distant %>%filter(ID != Follower_ID) %>% dplyr::select(ID, Follower_ID, ENTRY, EXIT, STAY_IN, Social_Group, L_time, Hour_ENTRY, time_between, FEED_INTK, PEN)data_PIC_pvalues60_distant
# Arrange the data and add a row numberdata_PIC_arranged2 <- data_PIC_pvalues120_distant %>%arrange(Social_Group, ENTRY, EXIT, by_group =TRUE) %>%mutate(line =row_number())dim(data_PIC_arranged2)
[1] 282428 30
# Filter the data and select specific columnsdata_PIC_filtered2 <- data_PIC_pvalues120_distant %>%filter(ID != Follower_ID) %>% dplyr::select(ID, Follower_ID, ENTRY, EXIT, STAY_IN, Social_Group, L_time, Hour_ENTRY, time_between, FEED_INTK, PEN)data_PIC_pvalues120_distant
# Arrange the data and add a row numberdata_PIC_arranged3 <- data_PIC_pvalues180_distant %>%arrange(Social_Group, ENTRY, EXIT, by_group =TRUE) %>%mutate(line =row_number())dim(data_PIC_arranged3)
[1] 257203 30
# Filter the data and select specific columnsdata_PIC_filtered3 <- data_PIC_pvalues180_distant %>%filter(ID != Follower_ID) %>% dplyr::select(ID, Follower_ID, ENTRY, EXIT, STAY_IN, Social_Group, L_time, Hour_ENTRY, time_between, FEED_INTK, PEN)data_PIC_pvalues180_distant
# Arrange the data and add a row numberdata_PIC_arranged4 <- data_PIC_pvalues240_distant %>%arrange(Social_Group, ENTRY, EXIT, by_group =TRUE) %>%mutate(line =row_number())dim(data_PIC_arranged4)
[1] 239488 30
# Filter the data and select specific columnsdata_PIC_filtered4 <- data_PIC_pvalues240_distant %>%filter(ID != Follower_ID) %>% dplyr::select(ID, Follower_ID, ENTRY, EXIT, STAY_IN, Social_Group, L_time, Hour_ENTRY, time_between, FEED_INTK, PEN)data_PIC_pvalues240_distant
# Arrange the data and add a row numberdata_PIC_arranged5 <- data_PIC_pvalues300_distant %>%arrange(Social_Group, ENTRY, EXIT, by_group =TRUE) %>%mutate(line =row_number())dim(data_PIC_arranged5)
[1] 224191 30
# Filter the data and select specific columnsdata_PIC_filtered5 <- data_PIC_pvalues300_distant %>%filter(ID != Follower_ID) %>% dplyr::select(ID, Follower_ID, ENTRY, EXIT, STAY_IN, Social_Group, L_time, Hour_ENTRY, time_between, FEED_INTK, PEN)data_PIC_pvalues300_distant
Filtering for social group number from 14-16 animals 60s immediate
#| warning: true#| echo: truesocial_group_summary <- data_PIC_filtered %>%group_by(Social_Group) %>%summarise(Unique_Animal_Count =n_distinct(Animal), # Count distinct IDs in each social groupfeeding_rec =n() # Count all records in each social group )# joining this summary back to the original data to keep all columnsextended_data <- data_PIC_filtered %>%left_join(social_group_summary, by ="Social_Group")# Filter the data where the Unique_Animal_Count is greater than 13 to keep 14,15,16 SGfiltered_data <-filter(extended_data, Unique_Animal_Count >13)final_summary <- filtered_data %>%summarize(Total_rec =sum(feeding_rec))final_summary
ggplot(filtered_data, aes(x= Unique_Animal_Count , y = feeding_rec, )) +geom_point()+geom_smooth()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Warning: Failed to fit group -1.
Caused by error in `smooth.construct.cr.smooth.spec()`:
! x has insufficient unique values to support 10 knots: reduce k.
Filtering for social group number from 14-16 animals 60s distant
social_group_summary1 <- data_PIC_filtered1 %>%group_by(Social_Group) %>%summarise(Unique_Animal_Count =n_distinct(Animal), # Count distinct IDs in each social groupfeeding_rec =n() # Count all records in each social group )# joining this summary back to the original data to keep all columnsextended_data1 <- data_PIC_filtered1 %>%left_join(social_group_summary1, by ="Social_Group")# Filter the data where the Unique_Animal_Count is greater than 13 to keep 14,15,16 SGfiltered_data1 <-filter(extended_data1, Unique_Animal_Count >13)final_summary1 <- filtered_data1 %>%summarize(Total_rec =sum(feeding_rec))final_summary1
ggplot(filtered_data1, aes(x= Unique_Animal_Count , y = feeding_rec, )) +geom_point()+geom_smooth()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Warning: Failed to fit group -1.
Caused by error in `smooth.construct.cr.smooth.spec()`:
! x has insufficient unique values to support 10 knots: reduce k.
Filtering for social group number from 14-16 animals 120s distant
social_group_summary2 <- data_PIC_filtered2 %>%group_by(Social_Group) %>%summarise(Unique_Animal_Count =n_distinct(Animal), # Count distinct IDs in each social groupfeeding_rec =n() # Count all records in each social group )# joining this summary back to the original data to keep all columnsextended_data2 <- data_PIC_filtered2 %>%left_join(social_group_summary2, by ="Social_Group")# Filter the data where the Unique_Animal_Count is greater than 13 to keep 14,15,16 SGfiltered_data2 <-filter(extended_data2, Unique_Animal_Count >13)final_summary2 <- filtered_data2 %>%summarize(Total_rec =sum(feeding_rec))final_summary2
ggplot(filtered_data2, aes(x= Unique_Animal_Count , y = feeding_rec, )) +geom_point()+geom_smooth()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Warning: Failed to fit group -1.
Caused by error in `smooth.construct.cr.smooth.spec()`:
! x has insufficient unique values to support 10 knots: reduce k.
Filtering for social group number from 14-16 animals 180s distant
social_group_summary3 <- data_PIC_filtered3 %>%group_by(Social_Group) %>%summarise(Unique_Animal_Count =n_distinct(Animal), # Count distinct IDs in each social groupfeeding_rec =n() # Count all records in each social group )# joining this summary back to the original data to keep all columnsextended_data3 <- data_PIC_filtered3 %>%left_join(social_group_summary3, by ="Social_Group")# Filter the data where the Unique_Animal_Count is greater than 13 to keep 14,15,16 SGfiltered_data3 <-filter(extended_data3, Unique_Animal_Count >13)final_summary3 <- filtered_data3 %>%summarize(Total_rec =sum(feeding_rec))final_summary3
ggplot(filtered_data3, aes(x= Unique_Animal_Count , y = feeding_rec, )) +geom_point()+geom_smooth()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Warning: Failed to fit group -1.
Caused by error in `smooth.construct.cr.smooth.spec()`:
! x has insufficient unique values to support 10 knots: reduce k.
Filtering for social group number from 14-16 animals 240s distant
social_group_summary4 <- data_PIC_filtered4 %>%group_by(Social_Group) %>%summarise(Unique_Animal_Count =n_distinct(Animal), # Count distinct IDs in each social groupfeeding_rec =n() # Count all records in each social group )# joining this summary back to the original data to keep all columnsextended_data4 <- data_PIC_filtered4 %>%left_join(social_group_summary4, by ="Social_Group")# Filter the data where the Unique_Animal_Count is greater than 13 to keep 14,15,16 SGfiltered_data4 <-filter(extended_data4, Unique_Animal_Count >13)final_summary4 <- filtered_data4 %>%summarize(Total_rec =sum(feeding_rec))final_summary4
ggplot(filtered_data4, aes(x= Unique_Animal_Count , y = feeding_rec, )) +geom_point()+geom_smooth()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Warning: Failed to fit group -1.
Caused by error in `smooth.construct.cr.smooth.spec()`:
! x has insufficient unique values to support 10 knots: reduce k.
Filtering for social group number from 14-16 animals 300s distant
social_group_summary5 <- data_PIC_filtered5 %>%group_by(Social_Group) %>%summarise(Unique_Animal_Count =n_distinct(Animal), # Count distinct IDs in each social groupfeeding_rec =n() # Count all records in each social group )# joining this summary back to the original data to keep all columnsextended_data5 <- data_PIC_filtered5 %>%left_join(social_group_summary5, by ="Social_Group")# Filter the data where the Unique_Animal_Count is greater than 13 to keep 14,15,16 SGfiltered_data5 <-filter(extended_data5, Unique_Animal_Count >13)final_summary5 <- filtered_data5 %>%summarize(Total_rec =sum(feeding_rec))final_summary5
ggplot(filtered_data5, aes(x= Unique_Animal_Count , y = feeding_rec, )) +geom_point()+geom_smooth()
`geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
Warning: Failed to fit group -1.
Caused by error in `smooth.construct.cr.smooth.spec()`:
! x has insufficient unique values to support 10 knots: reduce k.
Checking dimensions of filtered data for SGs
dim(filtered_data)
[1] 631867 10
dim(filtered_data1)
[1] 247648 10
dim(filtered_data2)
[1] 204769 10
dim(filtered_data3)
[1] 183079 10
dim(filtered_data4)
[1] 167922 10
dim(filtered_data5)
[1] 156076 10
Checking number of animals and records of filtered data for SGs
missing_animal_id <-setdiff(filtered_data5$Follower_ID, filtered_data5$Animal)# Output the missing Follower_IDmissing_animal_id
[1] 93432951
missing_records <- filtered_data5[filtered_data5$Follower_ID %in% missing_animal_id, ]# View these records to understand more about the contextmissing_records
#this is data filtered for follower follows itself, we dont want this data because it does not make sense calculate the social genetic effect of the animal itself. there is no social genetic effect. BUT THIS DATA STILL HAS THE SG THAT ARE LESS THAN 14. social_group_data_arr <- data_PIC_filtered %>%group_by(Social_Group) %>%summarise(Unique_Animal_Count =n_distinct(Animal),feeding_rec =n())social_group_data_arr
ggplot(social_group_data_arr, aes(x = feeding_rec)) +geom_histogram(aes(y = ..density..), binwidth =100, fill ="skyblue", color ="black") +# Adjust binwidth as necessarygeom_density(alpha = .2, fill ="#FF6666") +# Adds a density plotlabs(title ="Histogram and Density Plot of Animal Count",x ="Animal Count",y ="Density") +theme_minimal()
Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(density)` instead.
# Add mean and median lines to the histogram number ggplot(social_group_data_arr, aes(x = feeding_rec)) +geom_histogram(aes(y = ..density..), binwidth =100, fill ="skyblue", color ="black") +geom_density(alpha = .2, fill ="#FF6666") +geom_vline(aes(xintercept =mean(feeding_rec)), color ="green", linetype ="dashed", size =1) +geom_vline(aes(xintercept =median(feeding_rec)), color ="red", linetype ="dashed", size =1) +labs(title ="Histogram and Density Plot feeding records per Social Group",x ="Feeding Records", y ="Density") +theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: reciprocal condition number 1.0962e-15
Warning in simpleLoess(y, x, w, span, degree = degree, parametric = parametric,
: There are other near singularities as well. 1
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
16.07
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius
2.07
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
number 1.0962e-15
Warning in predLoess(object$y, object$x, newx = if (is.null(newdata)) object$x
else if (is.data.frame(newdata))
as.matrix(model.frame(delete.response(terms(object)), : There are other near
singularities as well. 1
SG_before <-unique(data_PIC_pvalues60$Social_Group)# Apply the filterdata_PIC_filtered <- data_PIC_pvalues60 %>%filter(ID != Follower_ID)# Get unique Social Groups after filteringSG_after <-unique(data_PIC_filtered$Social_Group)# Identify which SGs were removedSG_removed <-setdiff(SG_before, SG_after)SG_removed
[1] "B0106201218260219" "B0211160720210920"
# Find the records from the original dataset for the removed SGsremoved_records <- data_PIC_pvalues60 %>%filter(Social_Group %in% SG_removed)# View the removed records (optional)removed_records
##3 animals and 3 social groups were removed.#These animals likely had a large number of self-interactions, leading to a significant number of records being removed (~36k).#Even though it’s just 3 animals/groups, the total number of interactions per animal may be high.
The echo: false option disables the printing of code (only output is displayed).
Social Group frequency & hist
The
echo: falseoption disables the printing of code (only output is displayed).