Universidad Nacional de Costa Rica, Heredia, Costa Rica, Facultad de Ciencias Exactas y Naturales, Escuela de Ciencias Biologicas.

Se asumen la totalidad de supuestos estadisticos

Universidad Nacional de Costa Rica, Heredia, Costa Rica, Facultad de Ciencias Exactas y Naturales, Escuela de Ciencias Biologicas.

Se asumen la totalidad de supuestos estadisticos

library(readxl)
FASE_6_ANCOVA_2 <- read_excel("FASE-6-ANCOVA_2.xlsx")
View(FASE_6_ANCOVA_2)
DF <- data.frame(FASE_6_ANCOVA_2)

set.seed(2021)

n <- nrow(DF)
muestra <- sample(n,n* 0.80)# se toma una muestra aleatoria de 80%
df.train <- DF[muestra, ]
df.test <- DF[-muestra, ]

LDM <- df.test$LDM
SDM <- df.test$SDM
RDM <- df.test$RDM
TDM <- df.test$TDM
D2 <- df.test$D2
Deformation <- df.test$Deformation

#MODELO ANCOVA 1
lm.1<-lm(LDM~D2*Deformation,data=df.train)           
lm.1              
## 
## Call:
## lm(formula = LDM ~ D2 * Deformation, data = df.train)
## 
## Coefficients:
##       (Intercept)                 D2     DeformationYes  D2:DeformationYes  
##         0.2602733          0.0679758         -0.0010296         -0.0005135
summary(lm.1)
## 
## Call:
## lm(formula = LDM ~ D2 * Deformation, data = df.train)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.53083 -0.46717 -0.08022  0.30631  2.87361 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        0.2602733  0.3042009   0.856    0.395    
## D2                 0.0679758  0.0045465  14.951   <2e-16 ***
## DeformationYes    -0.0010296  0.4222119  -0.002    0.998    
## D2:DeformationYes -0.0005135  0.0068672  -0.075    0.941    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9995 on 76 degrees of freedom
## Multiple R-squared:  0.8417, Adjusted R-squared:  0.8354 
## F-statistic: 134.7 on 3 and 76 DF,  p-value: < 2.2e-16
#FULL MODEL
pv1 <- predict(lm.1, newdata = data.frame(LDM,D2,Deformation), interval = "prediction")
meanRPE <- mean(((pv1[,1]-LDM)/LDM)*100)
meanRPE
## [1] 16.78263
sd2RPE <- sd(((pv1[,1]-LDM)/LDM)*100)
sd2RPE
## [1] 41.79803
#REDUCED MODEL (No consideramos Deformation)

lmr.1<-lm(LDM~D2,data=df.train)           
lmr.1              
## 
## Call:
## lm(formula = LDM ~ D2, data = df.train)
## 
## Coefficients:
## (Intercept)           D2  
##     0.25621      0.06781
summary(lmr.1)
## 
## Call:
## lm(formula = LDM ~ D2, data = df.train)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.51021 -0.46795 -0.07718  0.29440  2.88742 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.256207   0.206841   1.239    0.219    
## D2          0.067809   0.003331  20.359   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9867 on 78 degrees of freedom
## Multiple R-squared:  0.8416, Adjusted R-squared:  0.8396 
## F-statistic: 414.5 on 1 and 78 DF,  p-value: < 2.2e-16
pv1.2 <- predict(lmr.1, newdata = data.frame(LDM,D2), interval = "prediction")
meanRPER <- mean(((pv1.2[,1]-LDM)/LDM)*100)
meanRPER
## [1] 16.77367
sd2RPER <- sd(((pv1.2[,1]-LDM)/LDM)*100)
sd2RPER
## [1] 41.80823
##########################################################

#MODELO 2 ANCOVA
lm.2<-lm(SDM~D2*Deformation,data=df.train)           
lm.2              
## 
## Call:
## lm(formula = SDM ~ D2 * Deformation, data = df.train)
## 
## Coefficients:
##       (Intercept)                 D2     DeformationYes  D2:DeformationYes  
##         -0.586328           0.039846           0.293565          -0.008372
summary(lm.2)
## 
## Call:
## lm(formula = SDM ~ D2 * Deformation, data = df.train)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.13881 -0.21032 -0.03857  0.18749  1.95663 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -0.586328   0.159041  -3.687 0.000424 ***
## D2                 0.039846   0.002377  16.763  < 2e-16 ***
## DeformationYes     0.293565   0.220740   1.330 0.187524    
## D2:DeformationYes -0.008372   0.003590  -2.332 0.022355 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5225 on 76 degrees of freedom
## Multiple R-squared:  0.8511, Adjusted R-squared:  0.8452 
## F-statistic: 144.8 on 3 and 76 DF,  p-value: < 2.2e-16
pv2 <- predict(lm.2, newdata = data.frame(SDM,D2,Deformation), interval = "prediction")
meanRPE2 <- mean(((pv2[,1]-SDM)/SDM)*100)
meanRPE2
## [1] -2.304031
sd2RPE2 <- sd(((pv2[,1]-SDM)/SDM)*100)
sd2RPE2
## [1] 171.1967
#REDUCED MODEL (No consideramos Deformation)

lmr.2<-lm(SDM~D2,data=df.train)           
lmr.2              
## 
## Call:
## lm(formula = SDM ~ D2, data = df.train)
## 
## Coefficients:
## (Intercept)           D2  
##    -0.46296      0.03648
summary(lmr.2)
## 
## Call:
## lm(formula = SDM ~ D2, data = df.train)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.03262 -0.29316 -0.04105  0.23185  2.16830 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.462962   0.112910   -4.10    1e-04 ***
## D2           0.036475   0.001818   20.06   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.5386 on 78 degrees of freedom
## Multiple R-squared:  0.8377, Adjusted R-squared:  0.8356 
## F-statistic: 402.5 on 1 and 78 DF,  p-value: < 2.2e-16
pv2.2 <- predict(lmr.2, newdata = data.frame(SDM,D2), interval = "prediction")
meanRPER2 <- mean(((pv2.2[,1]-SDM)/SDM)*100)
meanRPER2
## [1] -22.24571
sd2RPER2 <- sd(((pv2.2[,1]-SDM)/SDM)*100)
sd2RPER2
## [1] 215.8252
##########################################################

#MODELO 3 ANCOVA

lm.3<-lm(RDM~D2*Deformation,data=df.train)           
lm.3              
## 
## Call:
## lm(formula = RDM ~ D2 * Deformation, data = df.train)
## 
## Coefficients:
##       (Intercept)                 D2     DeformationYes  D2:DeformationYes  
##         -1.111639           0.103382           0.398477           0.009583
summary(lm.3)
## 
## Call:
## lm(formula = RDM ~ D2 * Deformation, data = df.train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6190 -0.8771 -0.0611  0.6187  3.8996 
## 
## Coefficients:
##                    Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -1.111639   0.402978  -2.759  0.00727 ** 
## D2                 0.103382   0.006023  17.165  < 2e-16 ***
## DeformationYes     0.398477   0.559309   0.712  0.47837    
## D2:DeformationYes  0.009583   0.009097   1.053  0.29548    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.324 on 76 degrees of freedom
## Multiple R-squared:  0.8822, Adjusted R-squared:  0.8776 
## F-statistic: 189.8 on 3 and 76 DF,  p-value: < 2.2e-16
pv3 <- predict(lm.3, newdata = data.frame(RDM,D2,Deformation), interval = "prediction")
meanRPE3 <- mean(((pv3[,1]-RDM)/RDM)*100)
meanRPE3
## [1] -0.7072322
sd2RPE3 <- sd(((pv3[,1]-RDM)/RDM)*100)
sd2RPE3
## [1] 70.07905
#REDUCED MODEL (No consideramos Deformation)

lmr.3<-lm(RDM~D2,data=df.train)           
lmr.3              
## 
## Call:
## lm(formula = RDM ~ D2, data = df.train)
## 
## Coefficients:
## (Intercept)           D2  
##     -0.8040       0.1057
summary(lmr.3)
## 
## Call:
## lm(formula = RDM ~ D2, data = df.train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.9712 -0.9829 -0.0301  0.7542  4.6328 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.803990   0.291599  -2.757  0.00726 ** 
## D2           0.105687   0.004696  22.508  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.391 on 78 degrees of freedom
## Multiple R-squared:  0.8666, Adjusted R-squared:  0.8649 
## F-statistic: 506.6 on 1 and 78 DF,  p-value: < 2.2e-16
pv3.2 <- predict(lmr.3, newdata = data.frame(RDM,D2), interval = "prediction")
meanRPER3 <- mean(((pv3.2[,1]-RDM)/RDM)*100)
meanRPER3
## [1] -2.310501
sd2RPER3 <- sd(((pv3.2[,1]-RDM)/RDM)*100)
sd2RPER3
## [1] 82.22218
##########################################################

#MODELO 4 ANCOVA

lm.4<-lm(TDM~D2*Deformation,data=df.train)           
lm.4              
## 
## Call:
## lm(formula = TDM ~ D2 * Deformation, data = df.train)
## 
## Coefficients:
##       (Intercept)                 D2     DeformationYes  D2:DeformationYes  
##        -1.4376934          0.2112037          0.6910122          0.0006972
summary(lm.4)
## 
## Call:
## lm(formula = TDM ~ D2 * Deformation, data = df.train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.9773 -1.5883 -0.2934  1.1806  6.5094 
## 
## Coefficients:
##                     Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       -1.4376934  0.6995822  -2.055   0.0433 *  
## D2                 0.2112037  0.0104558  20.200   <2e-16 ***
## DeformationYes     0.6910122  0.9709766   0.712   0.4788    
## D2:DeformationYes  0.0006972  0.0157928   0.044   0.9649    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.299 on 76 degrees of freedom
## Multiple R-squared:  0.9062, Adjusted R-squared:  0.9025 
## F-statistic: 244.8 on 3 and 76 DF,  p-value: < 2.2e-16
pv4 <- predict(lm.4, newdata = data.frame(TDM,D2,Deformation), interval = "prediction")
meanRPE4 <- mean(((pv4[,1]-TDM)/TDM)*100)
meanRPE4
## [1] 6.319529
sd2RPE4 <- sd(((pv4[,1]-TDM)/TDM)*100)
sd2RPE4
## [1] 38.14781
#REDUCED MODEL (No consideramos Deformation)

lmr.4<-lm(TDM~D2,data=df.train)           
lmr.4              
## 
## Call:
## lm(formula = TDM ~ D2, data = df.train)
## 
## Coefficients:
## (Intercept)           D2  
##      -1.011        0.210
summary(lmr.4)
## 
## Call:
## lm(formula = TDM ~ D2, data = df.train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.5169 -1.5956 -0.1005  1.1195  6.9438 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.010746   0.481723  -2.098   0.0391 *  
## D2           0.209971   0.007757  27.068   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.298 on 78 degrees of freedom
## Multiple R-squared:  0.9038, Adjusted R-squared:  0.9026 
## F-statistic: 732.7 on 1 and 78 DF,  p-value: < 2.2e-16
pv4.2 <- predict(lmr.4, newdata = data.frame(TDM,D2), interval = "prediction")
meanRPER4 <- mean(((pv4.2[,1]-TDM)/TDM)*100)
meanRPER4
## [1] 3.06318
sd2RPER4 <- sd(((pv4.2[,1]-TDM)/TDM)*100)
sd2RPER4
## [1] 41.51472

Figuras

library(ggplot2)
p <- ggplot(DF, (aes(x=`D2`, y=`TDM`, color=Deformation, shape=Deformation))) + theme_classic() 
p + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)

p1 <- ggplot(DF, (aes(x=`D2`, y=`LDM`, color=Deformation, shape=Deformation))) + theme_classic() 
p1 + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)

p2 <- ggplot(DF, (aes(x=`D2`, y=`RDM`, color=Deformation, shape=Deformation))) + theme_classic() 
p2 + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)

p <- ggplot(DF, (aes(x=`D2`, y=`SDM`, color=Deformation, shape=Deformation))) + theme_classic() 
p + geom_point() + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)