\[y=X\beta + Zu + e\] Where, \(y\) is a \(n x 1\) vector of visit length at the feeder (minutes), \(X\beta\) are the fixed effects, \(location-trial \ (12)\), \(hour \ entry \ at \ the \ feeder \ (23)\) and \(animal \ median \ weigth\) as covariate; \(Z\) is a \(n\) x \(q\) desing matrix (\(q\) is the number of pigs) relates records in \(y\) to the random vector of additive genetic effects \(u\) (\(q\) x \(1\)); \(e\) (\(n\) x \(1\)) is the random residuals vector.
tn<-60
trials60.data<-trials6.data%>%filter(to_next<=tn)
dim(trials60.data)
## [1] 50561 22
m0.60s<-lmer(visit.length ~ Loc_trial -1 + hour_entry+ wt_median+
(1|Eartag_trial),data=trials60.data)
a<-summary(m0.60s)
a$coefficients%>%as.data.frame()%>%kable(caption = "Estimates of fixed effects")%>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left", font_size = 12)
| Estimate | Std. Error | df | t value | Pr(>|t|) | |
|---|---|---|---|---|---|
| Loc_trialLoc_1_t_1 | 12.9939361 | 0.9283695 | 130.1510 | 13.9965131 | 0.0000000 |
| Loc_trialLoc_1_t_2 | 6.8821949 | 0.9453114 | 117.5850 | 7.2803471 | 0.0000000 |
| Loc_trialLoc_1_t_3 | 9.8871645 | 0.9441156 | 116.9905 | 10.4724091 | 0.0000000 |
| Loc_trialLoc_1_t_4 | 7.9543790 | 1.1003624 | 114.1788 | 7.2288722 | 0.0000000 |
| Loc_trialLoc_1_t_5 | 8.7565895 | 1.1057581 | 116.4309 | 7.9190825 | 0.0000000 |
| Loc_trialLoc_1_t_6 | 4.6620387 | 1.0363408 | 113.7009 | 4.4985576 | 0.0000166 |
| Loc_trialLoc_2_t_1 | 10.0389443 | 0.9234030 | 127.3816 | 10.8716825 | 0.0000000 |
| Loc_trialLoc_2_t_2 | 7.7116800 | 0.9462299 | 118.0423 | 8.1499012 | 0.0000000 |
| Loc_trialLoc_2_t_3 | 14.3440931 | 0.9457410 | 117.7969 | 15.1670411 | 0.0000000 |
| Loc_trialLoc_2_t_4 | 6.5965115 | 1.0984635 | 113.3943 | 6.0052166 | 0.0000000 |
| Loc_trialLoc_2_t_5 | 9.5063866 | 1.1079059 | 117.3356 | 8.5805002 | 0.0000000 |
| Loc_trialLoc_2_t_6 | 6.5742249 | 1.0372116 | 114.0833 | 6.3383645 | 0.0000000 |
| hour_entry1 | 0.1564490 | 0.3000460 | 50420.7705 | 0.5214166 | 0.6020789 |
| hour_entry2 | -0.0332462 | 0.3071040 | 50420.7992 | -0.1082570 | 0.9137923 |
| hour_entry3 | -0.1626509 | 0.3050358 | 50422.8651 | -0.5332190 | 0.5938843 |
| hour_entry4 | -0.5228390 | 0.2964313 | 50422.9903 | -1.7637780 | 0.0777754 |
| hour_entry5 | -0.7181708 | 0.2679597 | 50424.2606 | -2.6801452 | 0.0073614 |
| hour_entry6 | -0.8482063 | 0.2455589 | 50424.5424 | -3.4541875 | 0.0005524 |
| hour_entry7 | -1.3738684 | 0.2331936 | 50424.8310 | -5.8915353 | 0.0000000 |
| hour_entry8 | -2.6163535 | 0.2253249 | 50424.1176 | -11.6114726 | 0.0000000 |
| hour_entry9 | -1.2917203 | 0.2305038 | 50424.5159 | -5.6038993 | 0.0000000 |
| hour_entry10 | -0.3160773 | 0.2344594 | 50423.9117 | -1.3481106 | 0.1776289 |
| hour_entry11 | 0.2135334 | 0.2346562 | 50423.5890 | 0.9099837 | 0.3628354 |
| hour_entry12 | 0.6223754 | 0.2336280 | 50424.0072 | 2.6639595 | 0.0077251 |
| hour_entry13 | 0.3571469 | 0.2310535 | 50424.4604 | 1.5457326 | 0.1221754 |
| hour_entry14 | 0.4915259 | 0.2319728 | 50423.5399 | 2.1188945 | 0.0341043 |
| hour_entry15 | 0.6877607 | 0.2343857 | 50424.2395 | 2.9343110 | 0.0033444 |
| hour_entry16 | 1.2328088 | 0.2434844 | 50426.7489 | 5.0631940 | 0.0000004 |
| hour_entry17 | 1.4501870 | 0.2601850 | 50425.1231 | 5.5736766 | 0.0000000 |
| hour_entry18 | 1.1343611 | 0.2685797 | 50423.4858 | 4.2235555 | 0.0000241 |
| hour_entry19 | 0.9827557 | 0.2742579 | 50422.5596 | 3.5833271 | 0.0003396 |
| hour_entry20 | 0.5088402 | 0.2694470 | 50423.5553 | 1.8884616 | 0.0589697 |
| hour_entry21 | 0.3616686 | 0.2768654 | 50420.9365 | 1.3062975 | 0.1914574 |
| hour_entry22 | -0.0303011 | 0.2734622 | 50421.2263 | -0.1108053 | 0.9117712 |
| hour_entry23 | 0.5033409 | 0.2834251 | 50421.4028 | 1.7759222 | 0.0757518 |
| wt_median | 0.0180033 | 0.0017807 | 50426.5862 | 10.1100560 | 0.0000000 |
a0<-as.matrix(as.data.frame(VarCorr(m0.60s))[,4],1,2)
colnames(a0)<-"Variance"
rownames(a0)<-c("Eartag_M0","Residual_M0")
\[y=X\beta + Zu + Z_fa_f + e\] Where, \(y\) is a \(n x 1\) vector of visit length at the feeder (minutes), \(X\beta\) are the fixed effects, \(location-trial \ (12)\), \(hour \ entry \ at \ the \ feeder \ (23)\) and \(animal \ median \ weigth\) as covariate; \(Z\) is a \(n\) x \(q\) desing matrix (\(q\) is the number of pigs) relates records in \(y\) to the random vector of additive genetic effects \(u\) (\(q\) x \(1\)); \(Z_f\) is the design matrix of the next individual that visited the feeder, named \(followers\), relating to \(y\) with the random vector effects \(a_f\) (\(q\) x \(1\)) and \(e\) (\(n\) x \(1\)) is the random residuals vector.
m1.60s<-lmer(visit.length ~ Loc_trial -1 + hour_entry+ wt_median+
(1|Eartag_trial)+(1|follower_trial),data=trials60.data)
b<-summary(m1.60s)
b$coefficients%>%as.data.frame()%>%kable(caption = "Estimates of fixed effects")%>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F,
position = "left",font_size = 12)
| Estimate | Std. Error | df | t value | Pr(>|t|) | |
|---|---|---|---|---|---|
| Loc_trialLoc_1_t_1 | 12.9189431 | 0.9668462 | 157.5227 | 13.3619424 | 0.0000000 |
| Loc_trialLoc_1_t_2 | 6.7405261 | 0.9863427 | 143.4099 | 6.8338581 | 0.0000000 |
| Loc_trialLoc_1_t_3 | 9.6998386 | 0.9854199 | 142.8717 | 9.8433556 | 0.0000000 |
| Loc_trialLoc_1_t_4 | 7.8750545 | 1.1492205 | 139.7857 | 6.8525180 | 0.0000000 |
| Loc_trialLoc_1_t_5 | 8.8173600 | 1.1554416 | 142.8163 | 7.6311601 | 0.0000000 |
| Loc_trialLoc_1_t_6 | 4.6598961 | 1.0822455 | 139.1470 | 4.3057663 | 0.0000312 |
| Loc_trialLoc_2_t_1 | 10.3366703 | 0.9625252 | 154.7188 | 10.7391167 | 0.0000000 |
| Loc_trialLoc_2_t_2 | 7.6728380 | 0.9872163 | 143.9180 | 7.7721957 | 0.0000000 |
| Loc_trialLoc_2_t_3 | 14.3903789 | 0.9869773 | 143.7749 | 14.5802535 | 0.0000000 |
| Loc_trialLoc_2_t_4 | 6.3861547 | 1.1472215 | 138.8203 | 5.5666273 | 0.0000001 |
| Loc_trialLoc_2_t_5 | 9.3361062 | 1.1569163 | 143.5436 | 8.0698201 | 0.0000000 |
| Loc_trialLoc_2_t_6 | 6.8168684 | 1.0832754 | 139.6759 | 6.2928307 | 0.0000000 |
| hour_entry1 | 0.1544119 | 0.2966234 | 50336.2674 | 0.5205656 | 0.6026717 |
| hour_entry2 | -0.0552882 | 0.3037140 | 50340.3875 | -0.1820402 | 0.8555519 |
| hour_entry3 | -0.1990801 | 0.3017812 | 50348.0760 | -0.6596838 | 0.5094598 |
| hour_entry4 | -0.6312412 | 0.2936088 | 50372.5985 | -2.1499398 | 0.0315647 |
| hour_entry5 | -0.8163617 | 0.2657582 | 50388.5762 | -3.0718212 | 0.0021287 |
| hour_entry6 | -0.8192883 | 0.2436156 | 50384.1643 | -3.3630366 | 0.0007715 |
| hour_entry7 | -1.2774788 | 0.2312340 | 50380.3065 | -5.5246145 | 0.0000000 |
| hour_entry8 | -2.4893524 | 0.2232619 | 50370.3975 | -11.1499197 | 0.0000000 |
| hour_entry9 | -1.2347932 | 0.2284312 | 50375.6566 | -5.4055361 | 0.0000001 |
| hour_entry10 | -0.2365959 | 0.2322242 | 50371.6339 | -1.0188256 | 0.3082906 |
| hour_entry11 | 0.2870601 | 0.2324513 | 50371.6514 | 1.2349256 | 0.2168640 |
| hour_entry12 | 0.7382256 | 0.2314537 | 50369.9188 | 3.1895171 | 0.0014260 |
| hour_entry13 | 0.4917418 | 0.2288663 | 50368.3913 | 2.1485988 | 0.0316710 |
| hour_entry14 | 0.6263890 | 0.2297125 | 50364.4784 | 2.7268389 | 0.0063966 |
| hour_entry15 | 0.7469218 | 0.2321117 | 50365.1728 | 3.2179409 | 0.0012920 |
| hour_entry16 | 1.2564347 | 0.2413155 | 50376.8718 | 5.2066053 | 0.0000002 |
| hour_entry17 | 1.5051536 | 0.2577965 | 50368.2059 | 5.8385342 | 0.0000000 |
| hour_entry18 | 1.1388877 | 0.2660231 | 50362.0001 | 4.2811615 | 0.0000186 |
| hour_entry19 | 0.9963838 | 0.2715906 | 50356.7225 | 3.6686982 | 0.0002440 |
| hour_entry20 | 0.5877267 | 0.2666512 | 50354.2176 | 2.2041033 | 0.0275216 |
| hour_entry21 | 0.4420376 | 0.2738149 | 50341.0680 | 1.6143664 | 0.1064542 |
| hour_entry22 | 0.0733270 | 0.2705225 | 50347.1073 | 0.2710569 | 0.7863484 |
| hour_entry23 | 0.5664818 | 0.2802513 | 50340.6878 | 2.0213348 | 0.0432504 |
| wt_median | 0.0183021 | 0.0017694 | 50395.6998 | 10.3436108 | 0.0000000 |
b0<-as.matrix(as.data.frame(VarCorr(m1.60s))[,4],1,3)
colnames(b0)<-"Variance"
rownames(b0)<-c("Eartag_M1","Follower_M1","Residual_M1")
rbind(a0,b0)%>%kable(caption = "Estimated of variance components")%>%kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left",font_size = 12)
| Variance | |
|---|---|
| Eartag_M0 | 9.147135 |
| Residual_M0 | 41.673216 |
| Eartag_M1 | 8.936955 |
| Follower_M1 | 1.093803 |
| Residual_M1 | 40.669988 |
anova(m0.60s,m1.60s)
## refitting model(s) with ML (instead of REML)
## Data: trials60.data
## Models:
## m0.60s: visit.length ~ Loc_trial - 1 + hour_entry + wt_median + (1 |
## m0.60s: Eartag_trial)
## m1.60s: visit.length ~ Loc_trial - 1 + hour_entry + wt_median + (1 |
## m1.60s: Eartag_trial) + (1 | follower_trial)
## Df AIC BIC logLik deviance Chisq Chi Df Pr(>Chisq)
## m0.60s 38 332624 332960 -166274 332548
## m1.60s 39 331643 331988 -165783 331565 983.12 1 < 2.2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
The Average daily gain \(ADG\) for each individual was calculated as:
\[ADG_i=\frac{\ median(Wt_{initial}) \ - median(Wt_{end})}{total\ trial \ days}\] Where:
\(ADG_i\) is the Average daily gain in kilograms of an individual \(i\) with \(i=1,..,118\) in any trial.
\(median(Wt_{initial})\) is the median weight in the start day of any trial.
\(median(Wt_{end})\) is the median weight in the end day of any trial.
\(total\ trial \ days\) are the number of total days of each trial.
ggplot(ADG, aes(x=Loc_trial, y=ADG))+geom_boxplot()+
ggtitle("Average Daily Gain (kg) by Trial") +
xlab("Location by Trial")+ ylab("Average Daily Gain (Kg)")+
theme(axis.text.x = element_text(angle = 45, hjust = 1),legend.position="none")
\[y=X\beta + e\] Where:
\(y\) is a \(n x 1\) vector of Average daily gain (\(ADG_i\) kg) of an individual
\(X\beta\) are the fixed effects, \(location-trial \ (12)\)
\(e\) (\(n\) x \(1\)) is the random residuals vector.
and \(y_i-x_i\hat{\beta}=\hat{e_i}\) is the ADG of the animal \(i\)
# Average daily gain as deviation of group
adg.lm<-lm(ADG ~ Loc_trial-1, data = ADG)
summary(adg.lm)$coefficients%>%as.data.frame()%>%kable()%>%kable_styling()
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| Loc_trialLoc_1_t_1 | 0.8726667 | 0.0284775 | 30.64405 | 0 |
| Loc_trialLoc_1_t_2 | 0.8155569 | 0.0297438 | 27.41939 | 0 |
| Loc_trialLoc_1_t_3 | 1.1307487 | 0.0297438 | 38.01627 | 0 |
| Loc_trialLoc_1_t_4 | 0.8993560 | 0.0348777 | 25.78599 | 0 |
| Loc_trialLoc_1_t_5 | 1.0890625 | 0.0348777 | 31.22518 | 0 |
| Loc_trialLoc_1_t_6 | 0.8795238 | 0.0328830 | 26.74706 | 0 |
| Loc_trialLoc_2_t_1 | 0.9107778 | 0.0284775 | 31.98234 | 0 |
| Loc_trialLoc_2_t_2 | 0.8055718 | 0.0297438 | 27.08368 | 0 |
| Loc_trialLoc_2_t_3 | 1.0510695 | 0.0297438 | 35.33743 | 0 |
| Loc_trialLoc_2_t_4 | 1.0985078 | 0.0348777 | 31.49599 | 0 |
| Loc_trialLoc_2_t_5 | 1.0332031 | 0.0348777 | 29.62360 | 0 |
| Loc_trialLoc_2_t_6 | 0.9412698 | 0.0328830 | 28.62480 | 0 |
resi.adg<-as.data.frame(adg.lm$residuals)%>%
mutate(., ADG.locmean=adg.lm$residuals,
Eartag_trial=rownames(.))%>%select(ADG.locmean,Eartag_trial)
ADG<-left_join(ADG,resi.adg)
## Joining, by = "Eartag_trial"
The regression model for quantile level \(\tau\) of the Weight by individual is:
\[Q_{\tau}(y_i)= \beta_{0}(\tau) + \beta_1(\tau)x_{i1}\] \(\tau=0.5\) median quantile
\((y_i)\) is the \(i-th\) observation of the Weight of an animal
\(\beta_{0}\) is the intercept parameter
\(\beta_1\) is the slope
\(x_{i1}\) is the trial day
The goodness of fit for quantile regression by individual, which is analog to \(R^2\), was calculated following to Koenker and Machado (1999), and then
\[R^1(\tau)=1-\frac{\hat{V}(\tau)}{\tilde{V}(\tau)}\] Where:
\(R^1(\tau)\) is a local measure of goodness of fit for a particular quantile
\(\hat{V}(\tau)\) is a model with the intercept parameter only
\(\tilde{V}(\tau)\) is the unrestricted model
The Weight gain (\(weight.gain.qr\))
suppressMessages(library(quantreg))
# 3.1. Quantile regression function, to estimate ADG by trial day
qrf<-function(x){
# 1. Regression 0.5 quantile model, full model
rst1<-trials6.data%>% filter(Eartag_trial==x)%>%rq(Weight~trial.day,data=.,tau=0.5)
ADG<-rst1$coefficients[2]
rho1<-rst1$rho
# 2. Regression 0.5 quantile model, restricted model (intercept only)
rst0<-trials60.data%>% filter(Eartag_trial==x)%>%rq(Weight~1,data=.,tau=0.5)
rho0<-rst0$rho
# 3. Estimate R^2 by individual
R_sq.rq<- 1 - rho1/rho0
# 4. Output
out1<-tibble(ADG=ADG,R_sq.rq=R_sq.rq)
return(out1)
}
# 3.2. Mean Average Daily Gain Quantile Regression
mns<-unique(trials6.data$Eartag_trial)
wg<-sapply(mns,qrf)
wg<-as.matrix(t(wg))
Wg<-tibble(id=rownames(wg)%>%map_df(as_tibble), wg.qr=wg[,1]%>%map_df(as_tibble),
R.sq=wg[,2]%>%map_df(as_tibble))%>%
mutate(id=id$value, weight.gain.qr=wg.qr$value,
R.sq=R.sq$value)%>%select(id, weight.gain.qr, R.sq)
Wg%>%summary()%>%kable()%>%kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left",font_size = 12)
| id | weight.gain.qr | R.sq | |
|---|---|---|---|
| Length:118 | Min. :0.6000 | Min. :0.2593 | |
| Class :character | 1st Qu.:0.8585 | 1st Qu.:0.7920 | |
| Mode :character | Median :0.9664 | Median :0.8563 | |
| NA | Mean :0.9616 | Mean :0.8291 | |
| NA | 3rd Qu.:1.0800 | 3rd Qu.:0.8997 | |
| NA | Max. :1.2545 | Max. :0.9338 |
hist(Wg$weight.gain.qr, main = "Histogram Daily Weight Gain (kg)",
xlab = "Weight gain (kg)", col = "seagreen")
hist(Wg$R.sq, main = bquote(R^2~ "of regression 0.5 quantile on Animal Weight"),
xlab = bquote(R^2), col = "royalblue4")
# Animals with R square smaller and greater
Wg[Wg$R.sq<0.5,]
## # A tibble: 2 x 3
## id weight.gain.qr R.sq
## <chr> <dbl> <dbl>
## 1 1361_t_2 0.812 0.259
## 2 1132_t_3 0.6 0.389
Wg[Wg$R.sq>0.925,]
## # A tibble: 2 x 3
## id weight.gain.qr R.sq
## <chr> <dbl> <dbl>
## 1 1672_t_3 1.11 0.928
## 2 1413_t_3 1.12 0.934
trials6.data%>%filter(Eartag_trial%in%c("1361_t_2","1132_t_3","1672_t_3","1413_t_3"))%>%
ggplot(aes(x=trial.day, y=Weight)) + geom_point()+ facet_wrap(.~Eartag_trial) +
geom_quantile(quantiles = .5, size=0.75, color="chartreuse") +
theme_light()+
ggtitle("Body weight (kg) by Trial Day") + labs(subtitle = bquote(R^2~"= 0.259 1361_t_2;" ~ R^2~"= 0.388 1132_t_3;" ~ R^2~"= 0.927 1672_t_3;" ~ R^2~"= 0.933 1413_t_3"))+ xlab("Trial day")+ ylab("Weight (Kg)")+
theme(axis.text.x = element_text(angle = 45, hjust = 1),legend.position="none")
## Smoothing formula not specified. Using: y ~ x
## Smoothing formula not specified. Using: y ~ x
## Smoothing formula not specified. Using: y ~ x
## Smoothing formula not specified. Using: y ~ x
ADG<-left_join(ADG, Wg, by=c("Eartag_trial"="id"))
outADG.Blups<-left_join(ADG,BlupEF, by=c("Eartag_trial"="ID"))
The random regression model on median weight in each trial in matrix notation is:
\[Y_{j}= X_j\beta + Z_ju_{j} + \ e_{j}\]
Where:
\(Y_{ij}\) is a matrix \(n_j\)x\(1\) with all observation of median weight for each day, in \(jth\) trial
\(X_j\) is the design matrix \(n_j\)x\(2\) of fixed effects for each group
\(\beta\) is the fixed effects vector, with \(\beta=(\beta_{0}, \beta_{1})'\)
\(Z_j\) is the design matrix \(n_j\)x\(2\) of random effects for each group
\(u_j\) is the random effects vector, with \(u=(u_{0j}, u_{1j})'\)
\(e_{ij}\) is the error vector
and the the \(ith\) row (\(i = 1,...n_j\)) of the response vector is:
\[y_{ij}= \beta_0 + \beta_1x_{ij} + u_0 + u_{1j}x_{ij} + e_{ij}\]
# 4.1. Animal Median weight by day data
AmWT.day<-trials6.data%>%group_by(Loc_trial,Eartag_trial,trial.day)%>%
summarize(weightmd.day=median(Weight))%>%
ungroup()%>%as.data.frame()
# 4.2. Ramdom regresion model of median weight
mod_gr<-lmer(weightmd.day ~ 1+Loc_trial*trial.day+
(trial.day| Eartag_trial), data = AmWT.day,
control=lmerControl(optCtrl=list(maxfun=50000)))
# Estimates
b<-summary(mod_gr)
b$coefficients%>%as.data.frame()%>%kable(caption = "Estimates of fixed effects random regression model")%>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left",font_size = 12)
| Estimate | Std. Error | df | t value | Pr(>|t|) | |
|---|---|---|---|---|---|
| (Intercept) | 72.5398063 | 1.1965483 | 113.1047 | 60.6242166 | 0.0000000 |
| Loc_trialLoc_1_t_2 | -37.1575548 | 1.7094802 | 107.7843 | -21.7361720 | 0.0000000 |
| Loc_trialLoc_1_t_3 | -47.7608344 | 1.7092426 | 107.7237 | -27.9426882 | 0.0000000 |
| Loc_trialLoc_1_t_4 | -32.2843701 | 1.8682971 | 107.5656 | -17.2801048 | 0.0000000 |
| Loc_trialLoc_1_t_5 | -28.6484515 | 1.8806053 | 110.4273 | -15.2336337 | 0.0000000 |
| Loc_trialLoc_1_t_6 | -44.8310716 | 1.8024838 | 106.9799 | -24.8718302 | 0.0000000 |
| Loc_trialLoc_2_t_1 | -0.9827709 | 1.6923198 | 113.1433 | -0.5807241 | 0.5625814 |
| Loc_trialLoc_2_t_2 | -35.5552698 | 1.7094650 | 107.7805 | -20.7990627 | 0.0000000 |
| Loc_trialLoc_2_t_3 | -50.1831078 | 1.7085891 | 107.5598 | -29.3710806 | 0.0000000 |
| Loc_trialLoc_2_t_4 | -44.9280632 | 1.8681407 | 107.5296 | -24.0496138 | 0.0000000 |
| Loc_trialLoc_2_t_5 | -30.5662212 | 1.8806053 | 110.4273 | -16.2533956 | 0.0000000 |
| Loc_trialLoc_2_t_6 | -49.6295499 | 1.8024409 | 106.9697 | -27.5346343 | 0.0000000 |
| trial.day | 0.9210570 | 0.0336408 | 136.5452 | 27.3791300 | 0.0000000 |
| Loc_trialLoc_1_t_2:trial.day | -0.0985468 | 0.0466361 | 115.3914 | -2.1131035 | 0.0367470 |
| Loc_trialLoc_1_t_3:trial.day | 0.2008796 | 0.0466292 | 115.3213 | 4.3080198 | 0.0000348 |
| Loc_trialLoc_1_t_4:trial.day | -0.0059765 | 0.0507848 | 113.5056 | -0.1176838 | 0.9065262 |
| Loc_trialLoc_1_t_5:trial.day | 0.1665349 | 0.0518126 | 122.9643 | 3.2141783 | 0.0016710 |
| Loc_trialLoc_1_t_6:trial.day | -0.0029637 | 0.0490039 | 112.9625 | -0.0604785 | 0.9518814 |
| Loc_trialLoc_2_t_1:trial.day | -0.0253765 | 0.0475955 | 136.7720 | -0.5331706 | 0.5947808 |
| Loc_trialLoc_2_t_2:trial.day | -0.1113809 | 0.0466354 | 115.3846 | -2.3883346 | 0.0185478 |
| Loc_trialLoc_2_t_3:trial.day | 0.1299458 | 0.0465983 | 115.0189 | 2.7886355 | 0.0061959 |
| Loc_trialLoc_2_t_4:trial.day | 0.1689086 | 0.0507758 | 113.4256 | 3.3265571 | 0.0011855 |
| Loc_trialLoc_2_t_5:trial.day | 0.1377457 | 0.0518126 | 122.9643 | 2.6585360 | 0.0088920 |
| Loc_trialLoc_2_t_6:trial.day | 0.0252125 | 0.0490035 | 112.9590 | 0.5145037 | 0.6079051 |
# Variances
print(VarCorr(mod_gr), comp="Variance")
## Groups Name Variance Corr
## Eartag_trial (Intercept) 16.101555
## trial.day 0.011316 0.279
## Residual 3.312280
# fixed effects intercept and slope by trial
fxeff<-as.matrix(summary(mod_gr)$coef[,1])
fxeff<-tibble(Loc_trial=rownames(fxeff)[1:12],
intercept.rr=fxeff[1:12,],slope.rr=fxeff[13:24,])%>%
mutate(.,Loc_trial=str_sub(Loc_trial,start = 10,end = 21))
fxeff[1,1]<-"Loc_1_t_1"
# intercept group
fxeff$intercept.rr[-1]<-fxeff$intercept.rr[-1]+fxeff$intercept.rr[1] # sum means
# slope group
fxeff$slope.rr[-1]<-fxeff$slope.rr[-1]+fxeff$slope.rr[1]
fxeff%>%head()%>%kable(caption = "Intercept and Slope by group")%>%kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left",font_size = 12)
| Loc_trial | intercept.rr | slope.rr |
|---|---|---|
| Loc_1_t_1 | 72.53981 | 0.9210570 |
| Loc_1_t_2 | 35.38225 | 0.8225102 |
| Loc_1_t_3 | 24.77897 | 1.1219366 |
| Loc_1_t_4 | 40.25544 | 0.9150804 |
| Loc_1_t_5 | 43.89135 | 1.0875919 |
| Loc_1_t_6 | 27.70873 | 0.9180933 |
# Blups rr
blps.gr<-ranef(mod_gr)
blps.gr<-blps.gr[[1]]%>%mutate(ID=rownames(blps.gr[[1]]))%>%
select(ID, "(Intercept)", "trial.day" )
colnames(blps.gr)<-c("ID", "Eartag_blup.rr","trial.day.rr")
blps.gr<-left_join(outADG.Blups,blps.gr, by = c("Eartag_trial"="ID"))
blps.gr$Loc_trial<-as.character(blps.gr$Loc_trial)
blps.gr<-left_join(blps.gr,fxeff,"Loc_trial")
blps.gr<-mutate(blps.gr,Weight.gain.rr=trial.day.rr+slope.rr)
$weight.gain.rr_{pig}= group slope + trial.day.rr = (1 + u{1j})x_{ij} $
ggplot(blps.gr, aes(x=ADG, y=Weight.gain.rr))+geom_point() + geom_abline()+
labs(title = bquote( "Weight gain estimated by random regression and" ~ "ADG"[i] ~ "models" ) )+ xlab(bquote("ADG"[i]))+ ylab("Weight gain random regression")
The animal average daily intake was calculated as:
\[AFI_{i} = \frac{\sum_{j=1}^n X_j}{total\ days\ trial} \]
Where \(X_{j}\) is the \(jth\) consumed of an individual (\(j=1,..n\))
Feed.intake<-trials6.data%>%group_by(Loc_trial,Eartag_trial)%>%
summarize(animal.intake=sum(Consumed),
total.days=(max(trial.day)-min(trial.day)),
Average.daily.intake=animal.intake/total.days)%>%
ungroup()%>%as.data.frame()
rownames(Feed.intake)<-Feed.intake$Eartag_trial
#----------
ggplot(Feed.intake, aes(x=Loc_trial, y=Average.daily.intake))+geom_boxplot()+
ggtitle("Average Daily Feed Intake (kg) by Trial") +
xlab("Location by Trial")+ ylab("Average Daily Feed Intake (Kg)")+
theme(axis.text.x = element_text(angle = 45, hjust = 1),legend.position="none")
#----------
\[y=X\beta + e\] Where:
\(y\) is a \(n x 1\) vector of Average feed intake (\(AFI_i\) kg) of an individual
\(X\beta\) are the fixed effects, \(location-trial \ (12)\)
\(e\) (\(n\) x \(1\)) is the random residuals vector.
and \(y_i-x_i\hat{\beta}=\hat{e_i}\) is the AFI of the animal \(i\)
lmod.feed.intake<-lm(Average.daily.intake ~ Loc_trial-1, data = Feed.intake)
summary(lmod.feed.intake)$coefficients%>%as.data.frame()%>%kable(caption = "Estimated feed intake by location")%>%kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left",font_size = 12)
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| Loc_trialLoc_1_t_1 | 2.708833 | 0.0793264 | 34.14794 | 0 |
| Loc_trialLoc_1_t_2 | 2.183523 | 0.0828537 | 26.35395 | 0 |
| Loc_trialLoc_1_t_3 | 2.822786 | 0.0828537 | 34.06950 | 0 |
| Loc_trialLoc_1_t_4 | 2.416517 | 0.0971546 | 24.87290 | 0 |
| Loc_trialLoc_1_t_5 | 2.620605 | 0.0971546 | 26.97355 | 0 |
| Loc_trialLoc_1_t_6 | 2.212822 | 0.0915983 | 24.15791 | 0 |
| Loc_trialLoc_2_t_1 | 2.657176 | 0.0793264 | 33.49673 | 0 |
| Loc_trialLoc_2_t_2 | 2.128764 | 0.0828537 | 25.69303 | 0 |
| Loc_trialLoc_2_t_3 | 2.579544 | 0.0828537 | 31.13371 | 0 |
| Loc_trialLoc_2_t_4 | 2.221091 | 0.0971546 | 22.86140 | 0 |
| Loc_trialLoc_2_t_5 | 2.432934 | 0.0971546 | 25.04187 | 0 |
| Loc_trialLoc_2_t_6 | 2.232878 | 0.0915983 | 24.37686 | 0 |
resi.feed.intake<-as.data.frame(lmod.feed.intake$residuals)%>%
mutate(., feed.intake.fitloc=lmod.feed.intake$residuals,
Eartag_trial=rownames(.))%>%select(feed.intake.fitloc,Eartag_trial)
Feed.intake<-left_join(Feed.intake,resi.feed.intake, by="Eartag_trial")
Feed.intake$Loc_trial<-as.character(Feed.intake$Loc_trial)
# output file:weight gain, feed intake, Blups
ADGs.FI.BLUPs<-left_join(blps.gr,Feed.intake, by= c("Eartag_trial","Loc_trial", "total.days"))
\(EarBLUP = animal\ ramdom \ effect\ of \ visit \ length \ at \ the \ feeder\)
\(FollBLUP = animal\ ramdom \ effect\ as \ next \ individual \ that \ visited \ the \ feeder\)
\(ADG= ADG_i\)
\(ADG.locmean=ADG_i-\hat{\ loc:trial}\)
\(weight.gain.qr= estimated\ weight\ gain\ by\ median\ regression\ model\)
\(trial.day.rr = trial\ day\ random\ effect\ of\ the\ ith\ animal\)
\(Weight.gain.rr = group\ slope + trial.day.rr\)
\(Average.daily.intake= AFI_i\)
\(feed.intake.fitloc = AFI_i- \hat{\ loc:trial}\)
ADGs.FI.BLUPs%>%select(EarBLUP,FollBLUP,ADG,ADG.locmean,
weight.gain.qr, trial.day.rr, Weight.gain.rr,
Average.daily.intake, feed.intake.fitloc)%>%cor()%>%
kable()%>%kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F,
position = "left",font_size = 12)
| EarBLUP | FollBLUP | ADG | ADG.locmean | weight.gain.qr | trial.day.rr | Weight.gain.rr | Average.daily.intake | feed.intake.fitloc | |
|---|---|---|---|---|---|---|---|---|---|
| EarBLUP | 1.0000000 | 0.0523319 | 0.0669347 | 0.1036222 | 0.0340872 | 0.0772010 | 0.0529681 | 0.1306330 | 0.1761009 |
| FollBLUP | 0.0523319 | 1.0000000 | -0.0573553 | -0.0887921 | -0.0821592 | -0.1136392 | -0.0779686 | -0.0256262 | -0.0345455 |
| ADG | 0.0669347 | -0.0573553 | 1.0000000 | 0.6459500 | 0.9457667 | 0.6165763 | 0.9703293 | 0.6289405 | 0.5014952 |
| ADG.locmean | 0.1036222 | -0.0887921 | 0.6459500 | 1.0000000 | 0.6840328 | 0.9545264 | 0.6549066 | 0.5759162 | 0.7763684 |
| weight.gain.qr | 0.0340872 | -0.0821592 | 0.9457667 | 0.6840328 | 1.0000000 | 0.7240827 | 0.9891722 | 0.6627997 | 0.5701339 |
| trial.day.rr | 0.0772010 | -0.1136392 | 0.6165763 | 0.9545264 | 0.7240827 | 1.0000000 | 0.6861064 | 0.5771335 | 0.7780094 |
| Weight.gain.rr | 0.0529681 | -0.0779686 | 0.9703293 | 0.6549066 | 0.9891722 | 0.6861064 | 1.0000000 | 0.6487178 | 0.5337972 |
| Average.daily.intake | 0.1306330 | -0.0256262 | 0.6289405 | 0.5759162 | 0.6627997 | 0.5771335 | 0.6487178 | 1.0000000 | 0.7418079 |
| feed.intake.fitloc | 0.1761009 | -0.0345455 | 0.5014952 | 0.7763684 | 0.5701339 | 0.7780094 | 0.5337972 | 0.7418079 | 1.0000000 |
The sum of visit length total at the feeder for each individual was calculated as:
\[Vl_i= \sum_{j=1}^{n_i}X_j\]
Where:
\(Vl_i\) is the sum of the visit length total time at the feeder from the \(ith\) animal
\(X_j\) is the \(jth\) observation of visit length time at the feeder from the \(ith\) animal
\(n_i\) total number of observations from the \(ith\) animal
visitID.mean<-trials6.data%>%group_by(Eartag_trial, Loc_trial)%>%
summarize(vl=sum(visit.length))%>%ungroup()%>%as.data.frame()
rownames(visitID.mean)<-visitID.mean$Eartag_trial
summary(visitID.mean$vl.sum)
## Length Class Mode
## 0 NULL NULL
vlm1<-lm(vl ~ Loc_trial-1, data = visitID.mean)
summary(vlm1)$coefficients%>%as.data.frame()%>%kable(caption = "Mean estimated by location-trial group")%>%kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left",font_size = 12)
| Estimate | Std. Error | t value | Pr(>|t|) | |
|---|---|---|---|---|
| Loc_trialLoc_1_t_1 | 2056.836 | 293.9486 | 6.997264 | 0 |
| Loc_trialLoc_1_t_2 | 5481.055 | 307.0193 | 17.852474 | 0 |
| Loc_trialLoc_1_t_3 | 6575.724 | 307.0193 | 21.417949 | 0 |
| Loc_trialLoc_1_t_4 | 4858.150 | 360.0121 | 13.494409 | 0 |
| Loc_trialLoc_1_t_5 | 3241.925 | 360.0121 | 9.005045 | 0 |
| Loc_trialLoc_1_t_6 | 6372.876 | 339.4226 | 18.775637 | 0 |
| Loc_trialLoc_2_t_1 | 2169.504 | 293.9486 | 7.380556 | 0 |
| Loc_trialLoc_2_t_2 | 5314.229 | 307.0193 | 17.309102 | 0 |
| Loc_trialLoc_2_t_3 | 7418.902 | 307.0193 | 24.164282 | 0 |
| Loc_trialLoc_2_t_4 | 5624.471 | 360.0121 | 15.623007 | 0 |
| Loc_trialLoc_2_t_5 | 2991.698 | 360.0121 | 8.309993 | 0 |
| Loc_trialLoc_2_t_6 | 7056.528 | 339.4226 | 20.789798 | 0 |
visitID.mean$Loc_trial<-as.character(visitID.mean$Loc_trial)
The linear model fitted was: \[y=X\beta + e\] Where:
\(y\) is a \(n x 1\) vector of the sum of the visit length total \(Vl_i\) at the feeder from the \(ith\) animal
\(X\beta\) are the fixed effects, \(location-trial \ (12)\)
\(e\) (\(n\) x \(1\)) is the random residuals vector.
and \(y_i-x_i\hat{\beta}=\hat{e_i}\) is the visit length total as deviation of estimated mean of location-trial group of the animal \(i\)
visitl.fitloc<-as.data.frame(vlm1$residuals)%>%
mutate(., visitl.fitloc=vlm1$residuals,
Eartag_trial=rownames(.))%>%select(Eartag_trial,visitl.fitloc)
ADGs.FI.BLUPs<-left_join(ADGs.FI.BLUPs, visitID.mean, by=c("Eartag_trial","Loc_trial"))%>%
left_join(visitl.fitloc,by="Eartag_trial")
The random regression model on visit length in each trial in matrix notation is:
\[Y_{j}= X_j\beta + Z_ju_{j} + \ e_{j}\]
Where:
\(Y_{ij}\) is a matrix \(n_j\)x\(1\) with all observation of visit length total for each day, in \(jth\) trial
\(X_j\) is the design matrix \(n_j\)x\(2\) of fixed effects for each group (location-trial, trial day)
\(\beta\) is the fixed effects vector, with \(\beta=(\beta_{0}, \beta_{1})'\)
\(Z_j\) is the design matrix \(n_j\)x\(2\) of random effects for each group (Animal, trial day)
\(u_j\) is the random effects vector, with \(u=(u_{0j}, u_{1j})'\)
\(e_{ij}\) is the error vector
and the the \(ith\) row (\(i = 1,...n_j\)) of the response vector is:
\[y_{ij}= \beta_0 + \beta_1x_{ij} + u_0 + u_{1j}x_{ij} + e_{ij}\]
The visit length by animal is
\(Visit.length_{pig}= group\ slope + trial.day.rr = (\beta_1 + u_{1j})x_{ij}\)
vlmday<-trials6.data%>%group_by(Loc_trial,Eartag_trial, trial.day)%>%
summarize(visit.mean.day=sum(visit.length))%>%ungroup()
vlm.day.lm1<-lmer(visit.mean.day ~ 1 + Loc_trial*trial.day +
(trial.day| Eartag_trial), data = vlmday,
control=lmerControl(optCtrl=list(maxfun=50000)))
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl =
## control$checkConv, : Model failed to converge with max|grad| = 0.00497934
## (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
## - Rescale variables?
# Estimates
b<-summary(vlm.day.lm1)
b$coefficients%>%as.data.frame()%>%kable(caption = "Estimates of fixed effects")%>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F,
position = "left",font_size = 12)
| Estimate | Std. Error | df | t value | Pr(>|t|) | |
|---|---|---|---|---|---|
| (Intercept) | 100.3018761 | 7.1537101 | 138.2276 | 14.0209591 | 0.0000000 |
| Loc_trialLoc_1_t_2 | -7.6717385 | 9.9633882 | 118.9947 | -0.7699929 | 0.4428300 |
| Loc_trialLoc_1_t_3 | 10.1198647 | 9.9582432 | 118.7380 | 1.0162299 | 0.3115862 |
| Loc_trialLoc_1_t_4 | 5.6022364 | 10.8768700 | 118.2244 | 0.5150596 | 0.6074731 |
| Loc_trialLoc_1_t_5 | 8.3037668 | 11.1046157 | 128.4302 | 0.7477762 | 0.4559622 |
| Loc_trialLoc_1_t_6 | 5.6561064 | 10.4621031 | 116.1711 | 0.5406280 | 0.5897990 |
| Loc_trialLoc_2_t_1 | -2.8539053 | 10.1193546 | 138.3627 | -0.2820244 | 0.7783462 |
| Loc_trialLoc_2_t_2 | -11.8623821 | 9.9631075 | 118.9814 | -1.1906307 | 0.2361686 |
| Loc_trialLoc_2_t_3 | 17.2303586 | 9.9467934 | 118.2045 | 1.7322526 | 0.0858370 |
| Loc_trialLoc_2_t_4 | 11.8030578 | 10.8739555 | 118.0978 | 1.0854429 | 0.2799363 |
| Loc_trialLoc_2_t_5 | 11.8787315 | 11.1046157 | 128.4302 | 1.0697112 | 0.2867557 |
| Loc_trialLoc_2_t_6 | 25.0482761 | 10.4613007 | 116.1355 | 2.3943749 | 0.0182491 |
| trial.day | -1.0337949 | 0.1720591 | 512.9198 | -6.0083700 | 0.0000000 |
| Loc_trialLoc_1_t_2:trial.day | 0.8932592 | 0.2028248 | 231.5117 | 4.4040915 | 0.0000162 |
| Loc_trialLoc_1_t_3:trial.day | 0.8292139 | 0.2026083 | 230.0782 | 4.0926943 | 0.0000590 |
| Loc_trialLoc_1_t_4:trial.day | 0.6066495 | 0.2164930 | 210.4211 | 2.8021665 | 0.0055499 |
| Loc_trialLoc_1_t_5:trial.day | 0.6018994 | 0.2414749 | 323.5070 | 2.4925959 | 0.0131811 |
| Loc_trialLoc_1_t_6:trial.day | 0.6649735 | 0.2076507 | 204.5041 | 3.2023656 | 0.0015808 |
| Loc_trialLoc_2_t_1:trial.day | 0.3604554 | 0.2437310 | 515.9670 | 1.4789067 | 0.1397755 |
| Loc_trialLoc_2_t_2:trial.day | 0.9290088 | 0.2028077 | 231.4351 | 4.5807382 | 0.0000076 |
| Loc_trialLoc_2_t_3:trial.day | 0.7954158 | 0.2018820 | 227.2870 | 3.9400028 | 0.0001084 |
| Loc_trialLoc_2_t_4:trial.day | 0.8216961 | 0.2162648 | 209.5436 | 3.7994909 | 0.0001900 |
| Loc_trialLoc_2_t_5:trial.day | 0.1369993 | 0.2414749 | 323.5070 | 0.5673436 | 0.5708742 |
| Loc_trialLoc_2_t_6:trial.day | 0.4300173 | 0.2076408 | 204.4657 | 2.0709666 | 0.0396175 |
# Variances
print(VarCorr(vlm.day.lm1), comp="Variance")
## Groups Name Variance Corr
## Eartag_trial (Intercept) 497.02632
## trial.day 0.10954 -0.591
## Residual 359.34987
# fixed effects
fxeff<-as.matrix(summary(vlm.day.lm1)$coef[,1])
fxeff<-tibble(Loc_trial=rownames(fxeff)[1:12],
intercept.vl=fxeff[1:12,],slope.vl=fxeff[13:24,])%>%
mutate(.,Loc_trial=str_sub(Loc_trial,start = 10,end = 21))
fxeff[1,1]<-"Loc_1_t_1"
# intercetpt group
fxeff$intercept.vl[-1]<-fxeff$intercept.vl[-1]+fxeff$intercept.vl[1] # sum means
# slope group
fxeff$slope.vl[-1]<-fxeff$slope.vl[-1]+fxeff$slope.vl[1]
fxeff%>%head()%>%kable()%>%kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F, position = "left", font_size = 12)
| Loc_trial | intercept.vl | slope.vl |
|---|---|---|
| Loc_1_t_1 | 100.30188 | -1.0337949 |
| Loc_1_t_2 | 92.63014 | -0.1405357 |
| Loc_1_t_3 | 110.42174 | -0.2045810 |
| Loc_1_t_4 | 105.90411 | -0.4271453 |
| Loc_1_t_5 | 108.60564 | -0.4318955 |
| Loc_1_t_6 | 105.95798 | -0.3688214 |
# Blups visit length rr
blps.vl<-ranef(vlm.day.lm1)
blps.vl<-blps.vl[[1]]%>%mutate(ID=rownames(blps.vl[[1]]))%>%
select(ID, "(Intercept)", "trial.day")
colnames(blps.vl)<-c("ID", "EarBlup.vl.rr","vl.day")
blps.vl<-left_join(ADGs.FI.BLUPs,blps.vl, by = c("Eartag_trial"="ID"))
blps.vl$Loc_trial<-as.character(blps.vl$Loc_trial)
blps.vl<-left_join(blps.vl,fxeff,"Loc_trial")
output.vl<-mutate(blps.vl,Visit.length=vl.day+slope.vl)
\(EarBLUP = animal\ ramdom \ effect\ of \ visit \ length \ at \ the \ feeder\)
\(FollBLUP = animal\ ramdom \ effect\ as \ next \ individual \ that \ visited \ the \ feeder\)
\(trial.day.rr = trial\ day\ random\ effect\ of\ the\ ith\ animal\ on\ weight\ gain\)
\(feed.intake.fitloc = AFI_i- \hat{\ loc:trial}\)
\(visitl.fitloc = Vl_i-\hat{\ loc:trial}\)
\(vl.day = trial\ day\ random\ effect\ of\ the\ ith\ animal\ on\ visit\ length\ at\ the\ feeder\)
\(Visit.length_{pig}= group\ slope + vl.day = (\beta_1 + u_{1j})x_{ij}\)
kable(e1, caption = " Estimates correlation between BLUP's,visit length, weight gain and feed intake ") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"),full_width = F,
position = "left",font_size = 14) %>%
add_header_above(c(" " = 1, "Eartag Blup" = 2, "Follower Blup" = 2))
| Correlation | p-val | Correlation | p-val | |
|---|---|---|---|---|
| trial.day.rr | 0.0772010 | 0.4060127 | -0.1136392 | 0.2204732 |
| feed.intake.fitloc | 0.1761009 | 0.0564537 | -0.0345455 | 0.7103570 |
| visitl.fitloc | 0.4713046 | 0.0000001 | -0.2183611 | 0.0175252 |
| Visit.length | -0.0572525 | 0.5380226 | -0.1514855 | 0.1015259 |
| vl.day | -0.0863693 | 0.3523904 | -0.2285262 | 0.0128091 |
#output.vl%>%select("EarBLUP","FollBLUP","trial.day.rr","feed.intake.fitloc",
# "visitl.fitloc", "Visit.length","vl.day")%>%cor()