-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRegression_split.Rmd
More file actions
157 lines (124 loc) · 3.73 KB
/
Regression_split.Rmd
File metadata and controls
157 lines (124 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
title: "Regression_Training_Test"
author: "Andrea Huerfano"
date: "September 21, 2019"
output: pdf_document
header-includes:
- \usepackage{float}
- \floatplacement{figure}{H}
---
```{r, warning=FALSE, message=FALSE, results=FALSE}
rm(list = ls(all.names = TRUE))
Packages <- c("GLMsData", "dplyr","GGally", "MASS", "zoo", "lmtest")
lapply(Packages, library, character.only = TRUE)
set.seed(3)
source("Macros_Normal.txt")
```
```{r, message=FALSE, warning=FALSE}
AIS_f<-data(AIS)
```
This file is one of the regression analysis papers. Here you can find:
+ Plots
+ Correlations
+ Identifying the best model
+ Influence and leverage points
+ Residuals examination
+ Variance examination
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
colnames(AIS)
atletas<-subset(AIS,select = c("Sex","LBM", "Ht", "Wt", "BMI", "SSF"))
summary(atletas)
```
```{r}
ggpairs(atletas, columns = 1:ncol(atletas), title = "atlethes' variables behavior",
axisLabels = "show", mapping = aes(colour=Sex,alpha = 5),
upper = list(continuous = wrap("cor", size =3)),
lower = list(continuous = wrap("points", alpha = 0.9, size=0.3)))
```
```{r}
fit <- lm(LBM ~ 1+Ht+Wt+BMI+SSF, data=atletas) ##No va sex pq no es cuanti
Correlaciones(fit,2,1,4,"")
```
```{r, fig.height=7,fig.pos='h'}
Correlaciones.parcial(fit,4,1,1,"")
```
```{r}
train_index <- sample(1:nrow(atletas), 0.8 * nrow(atletas))
test_index <- setdiff(1:nrow(atletas), train_index)
# Build X_train and X_test
X_train <- atletas[train_index,]
X_test <- atletas[test_index,]
```
```{r}
cor(X_train[,2:ncol(X_train)])
```
```{r, warning=FALSE, message=FALSE}
fit <- lm(LBM ~ 1 + Sex+Ht+Wt+BMI+log(SSF), data=X_train)
stepAIC(fit)
```
```{r}
fit2<-lm(formula = LBM ~ Sex + Wt + BMI + log(SSF),data = X_train)
summary(fit2)
```
```{r, echo=FALSE, fig.height=6,fig.pos='h'}
######## Leverage #####################################
#par(mfrow=c(6,1))
Leverage <- Leverage.normal(fit2,3,"")
######## Residuos #####################################
residuos <- Residuos.normal(fit2,3,"")
######## Influencia #####################################
influence <- Influence.normal(fit2,2,2,3,"")
######## QQ Plot y sus bandas de confianza ########
qqplot.normal(fit2,500,0.01,3,"")
```
```{r}
######## Evaluando homogeneidad de la varianza ########
######## Test de Breusch-pagan ########
bptest(fit2) ##Nula es homocedasticidad
```
The Breusch-pagan's test to examine the homogenicity in the variance is not rejected with a significant level of 5%
```{r}
######## Evaluando NO correlación de los errores ########
######## Test de Durbin-Watson ########
dwtest(fit2)
```
The autocorrelation between residuals is rejected at significant level of 5%
```{r}
Residual<-fit2$residuals
rev<-as.data.frame(cbind(X_train$Wt,X_train$BMI,log(X_train$SSF),Residual))
colnames(rev)=c("Wt", "BMI", "SSF", "Residual")
ggplot(data=rev,aes(BMI,Residual))+geom_point()
ggplot(data=rev,aes(Wt,Residual))+geom_point()
ggplot(data=rev,aes(SSF,Residual))+geom_point()
```
```{r}
rev<-predict(fit2,X_test,type = "response")
X_test$predicted.classes<- round(rev,2)
X_test$indica<-'test'
X_train$predicted.classes<- round(fit2$fitted.values,2)
X_train$indica<-'train'
head(X_test,3)
tail(X_test,3)
```
```{r}
error<-X_test$LBM-X_test$predicted.classes
sqrt(mean(error^2))
quantile(error)
boxplot(error)
sd(error)
```
```{r}
inside<-X_train$LBM-fit2$fitted.values
sqrt(mean(error^2))/sqrt(mean(inside^2))
quantile(inside)
boxplot(inside)
sd(inside)
```
```{r}
total<-rbind(X_train,X_test)
colnames(total)
ggplot(total,aes(LBM,predicted.classes, color=indica))+geom_point()
```