Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
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:
library(lme4)
Loading required package: Matrix
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
library(RVAideMemoire)
*** Package RVAideMemoire v 0.9-81 ***
Attaching package: 'RVAideMemoire'
The following object is masked from 'package:lme4':
dummy
library(DHARMa)
This is DHARMa 0.4.4. For overview type '?DHARMa'. For recent changes, type news(package = 'DHARMa')
library(MuMIn)library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.1.3
Warning: package 'ggplot2' was built under R version 4.1.3
Warning: package 'tibble' was built under R version 4.1.3
Warning: package 'tidyr' was built under R version 4.1.3
Warning: package 'readr' was built under R version 4.1.3
Warning: package 'purrr' was built under R version 4.1.3
Warning: package 'dplyr' was built under R version 4.1.3
Warning: package 'stringr' was built under R version 4.1.3
Warning: package 'forcats' was built under R version 4.1.3
Warning: package 'lubridate' was built under R version 4.1.3
-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
v dplyr 1.1.2 v readr 2.1.4
v forcats 1.0.0 v stringr 1.5.0
v ggplot2 3.4.2 v tibble 3.2.1
v lubridate 1.9.2 v tidyr 1.3.0
v purrr 1.0.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x tidyr::expand() masks Matrix::expand()
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
x tidyr::pack() masks Matrix::pack()
x dplyr::recode() masks car::recode()
x purrr::some() masks car::some()
x tidyr::unpack() masks Matrix::unpack()
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(glmmTMB)
Warning in checkMatrixPackageVersion(): Package version inconsistency detected.
TMB was built with Matrix version 1.4.0
Current Matrix version is 1.3.4
Please re-install 'TMB' from source using install.packages('TMB', type = 'source') or ask CRAN for a binary version of 'TMB' matching CRAN's 'Matrix' package
ID LINE SIRE DAM
Min. :96251326 Min. :65 Min. :87591163 Min. :86315923
1st Qu.:96601065 1st Qu.:65 1st Qu.:89122431 1st Qu.:90888369
Median :97257792 Median :65 Median :90896190 Median :92297569
Mean :97267678 Mean :65 Mean :90604058 Mean :91786593
3rd Qu.:97887849 3rd Qu.:65 3rd Qu.:92013824 3rd Qu.:93218718
Max. :98368540 Max. :65 Max. :93693188 Max. :94232962
LITTER PEN FARM ENTRY_TIME
Min. :78043165 Length:114263 Min. :774 Length:114263
1st Qu.:78261882 Class :character 1st Qu.:774 Class :character
Median :78637026 Mode :character Median :774 Mode :character
Mean :78609475 Mean :774
3rd Qu.:78945651 3rd Qu.:774
Max. :79177365 Max. :774
EXIT_TIME STAY_IN FEED_INTK ENTRY_WT
Length:114263 Min. : 5 Min. :-3543.0 Min. :-913
Class :character 1st Qu.: 576 1st Qu.: 245.0 1st Qu.: 817
Mode :character Median : 1315 Median : 593.0 Median :1117
Mean : 1402 Mean : 638.9 Mean :1167
3rd Qu.: 2038 3rd Qu.: 947.0 3rd Qu.:1480
Max. :10169 Max. : 9102.0 Max. :9090
EXIT_WT FEEDER_NO START_DAY OFFTEST_DAY
Min. :-913.0 Min. : 4.00 Length:114263 Length:114263
1st Qu.: 414.0 1st Qu.:13.00 Class :character Class :character
Median : 516.0 Median :31.00 Mode :character Mode :character
Mean : 528.1 Mean :28.65
3rd Qu.: 635.0 3rd Qu.:46.00
Max. :3917.0 Max. :64.00
# Assuming your dataset is named "data"data$PEN <-as.factor(data$PEN)data$START <-as.factor(data$START_DAY)# Create the new variable PEN_START by pasting PEN and START togetherdata$PEN_START <-paste(data$PEN, data$START_DAY, sep ="_")# Perform linear regressionFEED_INTK_model <-lm(FEED_INTK ~ PEN_START, data = data)# Print the summary of the regression modelsummary(FEED_INTK_model)
# Assuming your dataset is named "data"data$TIME <-paste(data$PEN, data$START_DAY, data$OFFTEST_DAY, sep ="_")library(dplyr)# Assuming your dataset is named "data"data_summary <- data %>%group_by(TIME) %>%summarize(Mean_FEED_INTK =mean(FEED_INTK),Median_FEED_INTK =median(FEED_INTK),SD_FEED_INTK =sd(FEED_INTK),Num_Animals =n(),Num_Unique_PEN =n_distinct(PEN) )print(data_summary)
#######################TRYING DIFFERENT APPROACH##########################library(dplyr)library(lubridate)# Convert date columns to Date formatdata$START_DAY <-ymd(data$START_DAY) # Assuming 'ymd' is the correct format, adjust if neededdata$OFFTEST_DAY <-ymd(data$OFFTEST_DAY)# Define the date rangesdate_range1 <-c("2022-10-27", "2023-01-02")date_range2 <-c("2023-01-05", "2023-03-15")date_range3 <-c("2023-03-16", "2023-05-15")# Convert date ranges to Date objectsdate_range1 <-ymd(date_range1)date_range2 <-ymd(date_range2)date_range3 <-ymd(date_range3)# Subset the data based on date rangesdata_subset1 <- data %>%filter(START_DAY >= date_range1[1] & OFFTEST_DAY <= date_range1[2])data_subset2 <- data %>%filter(START_DAY >= date_range2[1] & OFFTEST_DAY <= date_range2[2])data_subset3 <- data %>%filter(START_DAY >= date_range3[1] & OFFTEST_DAY <= date_range3[2])head(data)
# Create linear regression models for each subsetmodel1 <-lm(FEED_INTK ~ TIME, data = data_subset1)model2 <-lm(FEED_INTK ~ TIME, data = data_subset2)model3 <-lm(FEED_INTK ~ TIME, data = data_subset3)# ... rest of the code for plotting ...library(ggplot2)# Plot for date range 1ggplot(data_subset1, aes(x = TIME, y = FEED_INTK)) +geom_boxplot() +labs(title ="Boxplots of FEED_INTK for Date Range 1",x ="Social Group", y ="FEED_INTK")
# Plot for date range 2ggplot(data_subset2, aes(x = TIME, y = FEED_INTK)) +geom_boxplot() +labs(title ="Boxplots of FEED_INTK for Date Range 2",x ="Social Group", y ="FEED_INTK")
# Plot for date range 3ggplot(data_subset3, aes(x = TIME, y = FEED_INTK)) +geom_boxplot() +labs(title ="Boxplots of FEED_INTK for Date Range 3",x ="Social Group", y ="FEED_INTK")
library(ggplot2)# Convert PEN column to numeric#data$PEN <- as.numeric(data$PEN)# Categorize PEN as a factor with 15 groupsdata$PEN <-as.factor(data$PEN)data$ID <-as.factor(data$ID)# Plotting boxplotsggplot(data = data, aes(x= PEN, y= FEED_INTK))+geom_boxplot(outlier.shape =1)+theme(axis.text.x =element_text(hjust =1))+labs(title ="Boxplot of FEED_INTK by PEN")+labs(y="FEED_INTAKE")+labs(x="PEN")+theme(legend.position ="none")
ggplot(data = data, aes(x= PEN, y= STAY_IN))+geom_boxplot(outlier.shape =1)+theme(axis.text.x =element_text(hjust =1))+labs(title ="Boxplot of STAY_IN by PEN")+labs(y="STAY_IN")+labs(x="PEN")+theme(legend.position ="none")
# Plot scatter plot with smooth curves for FEED_INTK and STAY_INlibrary(ggplot2)ggplot(data, aes(y = STAY_IN, x = FEED_INTK)) +geom_point() +facet_wrap(~PEN)+geom_smooth(method ="gam", se =FALSE, color ="blue") +labs(title ="Scatter Plot with Smooth Curves",x ="STAY_IN", y ="FEED_INTK") +theme_minimal()
`geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'
library(ggplot2)filter(data, PEN=="B0502") %>%ggplot(aes(y = STAY_IN, x = FEED_INTK, color=ID)) +geom_point() +facet_wrap(~PEN)+geom_smooth(method ="gam", se =TRUE) +labs(title ="Scatter Plot with Smooth Curves",x ="STAY_IN", y ="FEED_INTK") +theme_minimal()
`geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'
#I will break down the output from the linear regression model summary:#1. Residual Standard Error: This is the estimated standard deviation of the residuals, which are the differences between the observed values and the predicted values from the regression model. In this case, the residual standard error is approximately 479.7.#2. Degrees of Freedom (DF): The first value after the residual standard error (114227) represents the total degrees of freedom, which is the number of data points minus the number of estimated coefficients in the model.#3. Multiple R-squared: This is a measure of how well the independent variables (PEN_START) explain the variation in the dependent variable (FEED_INTK). It ranges from 0 to 1, with 1 indicating that the model explains all the variation, and 0 indicating that the model explains none of the variation. In this case, the multiple R-squared is approximately 0.0205, suggesting that the model explains only about 2.05% of the variation in FEED_INTK.#4. Adjusted R-squared: This is a modified version of the R-squared that takes into account the number of independent variables in the model and adjusts for the degrees of freedom. It penalizes the model for including additional variables that may not contribute significantly to the explanation of the dependent variable. In this case, the adjusted R-squared is approximately 0.0202.#5. F-statistic: This is a test statistic that measures whether the overall regression model is statistically significant. It compares the variation explained by the model to the variation that cannot be explained by the model. In this case, the F-statistic is 68.29, and it has 35 and 114227 degrees of freedom for the numerator and denominator, respectively.#6. p-value: This is the probability of obtaining an F-statistic as extreme as the one observed, assuming that the null hypothesis is true (i.e., the model has no explanatory power). In this case, the p-value is less than 2.2e-16, which is very close to zero. This extremely small p-value indicates that the regression model is highly statistically significant, suggesting that at least some of the independent variables (PEN_START) have a significant effect on the dependent variable (FEED_INTK).#In summary, the linear regression model explains a small portion of the variation in the dependent variable (FEED_INTK), but it is statistically significant, indicating that at least some of the independent variables (PEN_START) are significantly associated with FEED_INTK. However, the overall explanatory power of the model is relatively low (R-squared is low), suggesting that other factors not included in the model may also influence FEED_INTK.
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).