#UNIVERSIDAD NACIONAL DEL ALTIPLANO
#REGRESION AVANZADA
#FINESI
#TEMA: Variables Dummy

#Caso1
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.2
Caso1 <- read_excel("D:/VII-RA/Caso1.xlsx")
Caso1
## # A tibble: 26 x 4
##    Individuo    CP    GIM Grupo
##        <dbl> <dbl>  <dbl> <dbl>
##  1         1  -4    -7.44     0
##  2         2  -5.2  -6.29     0
##  3         3  -9.2  -8.99     0
##  4         4  -5.9  -4.27     0
##  5         5  -7.2  -8.08     0
##  6         6  -6.3 -10.5      0
##  7         7  -4.7  -3.11     0
##  8         8  -9.3  -6.66     0
##  9         9  -4.9  -5.75     0
## 10        10   0.4  -5.33     0
## # ... with 16 more rows
View(Caso1)

Grupo <- as.factor(Caso1$Grupo)
Grupo
##  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
## Levels: 0 1
#VARIABLES
y<- Caso1$CP
y
##  [1]  -4.0  -5.2  -9.2  -5.9  -7.2  -6.3  -4.7  -9.3  -4.9   0.4  -2.7 -10.4
## [13]  -1.7   0.2  -2.7  -2.8  -1.8  -2.2  -0.3  -0.9  -0.4  -1.7  -2.7   1.3
## [25]   1.0   0.3
x<- Caso1$GIM
x
##  [1]  -7.4420  -6.2894  -8.9897  -4.2663  -8.0755 -10.5133  -3.1076  -6.6595
##  [9]  -5.7514  -5.3274 -10.5106 -14.9994  -2.5526  -0.9783   0.0000   0.0000
## [17]   0.4440   1.3548  -0.9278  -0.7464   1.9881  -0.9783   1.3591   0.9031
## [25]  -1.4125   0.1430
#MODELO
mod<- lm(y~x+Grupo)
mod
## 
## Call:
## lm(formula = y ~ x + Grupo)
## 
## Coefficients:
## (Intercept)            x       Grupo1  
##     -2.4374       0.4171       1.4236
#ANOVA
summary(mod)
## 
## Call:
## lm(formula = y ~ x + Grupo)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.0846 -1.6854 -0.1773  1.4699  5.0597 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)  -2.4374     1.4432  -1.689   0.1047  
## x             0.4171     0.1794   2.325   0.0293 *
## Grupo1        1.4236     1.5832   0.899   0.3779  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.229 on 23 degrees of freedom
## Multiple R-squared:  0.5804, Adjusted R-squared:  0.5439 
## F-statistic: 15.91 on 2 and 23 DF,  p-value: 4.602e-05
#GRAFICO Y VARIABLE DUMMY
plot(y~x,pch=as.character(Grupo))
coef(mod)
## (Intercept)           x      Grupo1 
##  -2.4373979   0.4171451   1.4236121
abline(-2.4373979, 0.4171451, col="blue")
abline(-2.4373979, 1.4236121, col="red")

#Caso2
library(readxl)
Caso2 <- read_excel("D:/VII-RA/Caso2.xlsx")
Caso2
## # A tibble: 15 x 4
##    Individuo     y     x  Sexo
##        <dbl> <dbl> <dbl> <dbl>
##  1         1  41   1.05      1
##  2         2  46.2 0.46      1
##  3         3  44.3 0.580     1
##  4         4  53.1 0.7       1
##  5         5  57.8 1.07      1
##  6         6  48.4 0.68      1
##  7         7  31.3 0.71      1
##  8         8  39.6 0.87      1
##  9         9  21.8 0.73      0
## 10        10  49.1 0.72      0
## 11        11  47.4 0.82      0
## 12        12  27.3 0.54      0
## 13        13  39.7 0.580     0
## 14        14  48.5 1.53      0
## 15        15  39.7 0.53      0
View(Caso2)

Sexo <- as.factor(Caso2$Sexo)
Sexo
##  [1] 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0
## Levels: 0 1
#VARIABLES
y<- Caso2$y
y
##  [1] 41.0 46.2 44.3 53.1 57.8 48.4 31.3 39.6 21.8 49.1 47.4 27.3 39.7 48.5 39.7
x<- Caso2$x
x
##  [1] 1.05 0.46 0.58 0.70 1.07 0.68 0.71 0.87 0.73 0.72 0.82 0.54 0.58 1.53 0.53
#MODELO
mod<- lm(y~x+Sexo)
mod
## 
## Call:
## lm(formula = y ~ x + Sexo)
## 
## Coefficients:
## (Intercept)            x        Sexo1  
##      30.156       11.451        6.296
#ANOVA
summary(mod)
## 
## Call:
## lm(formula = y ~ x + Sexo)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -16.715  -7.146   2.902   6.167  10.699 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)   30.156      7.853   3.840  0.00235 **
## x             11.451      9.024   1.269  0.22854   
## Sexo1          6.296      4.804   1.311  0.21451   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9.279 on 12 degrees of freedom
## Multiple R-squared:  0.2129, Adjusted R-squared:  0.08168 
## F-statistic: 1.623 on 2 and 12 DF,  p-value: 0.2378
#GRAFICO Y VARIABLE DUMMY
plot(y~x,pch=as.character(Sexo))
coef(mod)
## (Intercept)           x       Sexo1 
##   30.155834   11.451223    6.296481
abline(30.155834, 11.451223, col="blue")
abline(30.155834, 6.296481, col="red")

#Caso3
library(readxl)
Caso3 <- read_excel("D:/VII-RA/Caso3.xlsx")
Caso3
## # A tibble: 28 x 3
##    Biorretroalimentacion Calificacion Numero
##    <chr>                        <dbl>  <dbl>
##  1 Si                             255     88
##  2 Si                              88    102
##  3 No                             162     73
##  4 Si                              90    105
##  5 No                             245     51
##  6 Si                             150     52
##  7 Si                              87    106
##  8 Si                             212     76
##  9 Si                             112    100
## 10 Si                              77    112
## # ... with 18 more rows
View(Caso3)

Biorretroalimentacion <- as.factor(Caso3$Biorretroalimentacion)
Biorretroalimentacion
##  [1] Si Si No Si No Si Si Si Si Si No No No Si No Si No Si No Si No No No No Si
## [26] Si No Si
## Levels: No Si
#VARIABLES
y<- Caso3$Numero
y
##  [1]  88 102  73 105  51  52 106  76 100 112  89  52  49  75  50  75  75 112  55
## [20] 115  50  87 106  91  75  70 100 100
x<- Caso3$Calificacion
x
##  [1] 255  88 162  90 245 150  87 212 112  77 137 171 199 137 149 251 102  90 180
## [20]  25 142  88  87 101 211 136 100 100
#MODELO
mod<- lm(y~x+Biorretroalimentacion)
mod
## 
## Call:
## lm(formula = y ~ x + Biorretroalimentacion)
## 
## Coefficients:
##             (Intercept)                        x  BiorretroalimentacionSi  
##                 105.355                   -0.237                   17.450
#ANOVA
summary(mod)
## 
## Call:
## lm(formula = y ~ x + Biorretroalimentacion)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -35.248  -8.061   2.977   7.983  25.642 
## 
## Coefficients:
##                          Estimate Std. Error t value Pr(>|t|)    
## (Intercept)             105.35487    7.96910  13.220 8.76e-13 ***
## x                        -0.23704    0.04788  -4.951 4.23e-05 ***
## BiorretroalimentacionSi  17.44955    5.55260   3.143  0.00428 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 14.61 on 25 degrees of freedom
## Multiple R-squared:  0.5961, Adjusted R-squared:  0.5637 
## F-statistic: 18.44 on 2 and 25 DF,  p-value: 1.2e-05
#GRAFICO Y VARIABLE DUMMY
plot(y~x,pch=as.character(Biorretroalimentacion))
coef(mod)
##             (Intercept)                       x BiorretroalimentacionSi 
##             105.3548742              -0.2370442              17.4495491
abline(105.3548742, -0.2370442, col="blue")
abline(105.3548742, 17.4495491, col="red")