library(readr)
library(dplyr)
library(ggplot2)
library(caret)
datos_fifa <- read.csv("C:/Users/cinth/Documents/ITD/Analisis inteligente de datos/Datos/data.csv", encoding = "UTF-8")
datos_esp <- select(datos_fifa, Overall, Value)
print('Variable independiente x: Overall, significa como se valora un jugador')
[1] "Variable independiente x: Overall, significa como se valora un jugador"
print('Variable dependiente y: es el valor económico de un jugador')
[1] "Variable dependiente y: es el valor económico de un jugador"
mk_to_pesos <- function(m_k) {
options(scipen=999)
pesos <- substr(m_k,2,nchar(m_k)-1)
pesos <- as.numeric(pesos)
pesos
}
datos_esp <- datos_esp %>%
mutate(datos_esp, Valor = ifelse(substr(Value, nchar(Value), nchar(Value)) == 'M', mk_to_pesos(Value)*1000000, mk_to_pesos(Value)*100)) %>%
filter(Valor > 0)
4.1 Visualizar la dispersión de los datos:
ggplot(datos_esp, aes(x = Overall, y = Valor)) +
geom_point()
set.seed(2020)
entrenamiento <- createDataPartition(y = datos_esp$Valor, p = 0.7, list = FALSE, times = 1)
datos_entrenamiento <- datos_esp[entrenamiento, ]
datos_validacion <- datos_esp[-entrenamiento, ]
head(datos_entrenamiento,10)
Overall Value Valor
1 94 \200110.5M 110500000
2 94 \20077M 77000000
3 92 \200118.5M 118500000
5 91 \200102M 102000000
6 91 \20093M 93000000
7 91 \20067M 67000000
8 91 \20080M 80000000
11 90 \20077M 77000000
12 90 \20076.5M 76500000
13 90 \20044M 44000000
head(datos_validacion,10)
Overall Value Valor
4 91 \20072M 72000000
9 91 \20051M 51000000
10 90 \20068M 68000000
16 89 \20089M 89000000
22 89 \20060M 60000000
23 89 \20038M 38000000
25 89 \20027M 27000000
29 88 \20069.5M 69500000
30 88 \20062M 62000000
31 88 \20073.5M 73500000
5.1 - Indice de correlación:
indice_correlacion <- cor(x = datos_entrenamiento$Overall, y = datos_entrenamiento$Valor, method = "pearson")
indice_correlacion
[1] 0.6269567
cor.test(datos_entrenamiento$Overall, datos_entrenamiento$Valor)
Pearson's product-moment correlation
data: datos_entrenamiento$Overall and datos_entrenamiento$Valor
t = 90.216, df = 12567, p-value < 0.00000000000000022
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.6162285 0.6374523
sample estimates:
cor
0.6269567
5.2 . Coeficiente de determinación (R Square):
coef_det <- indice_correlacion ^ 2
coef_det
[1] 0.3930747
modelo <- lm(formula = Valor ~ Overall, data = datos_entrenamiento)
summary(modelo)
Call:
lm(formula = Valor ~ Overall, data = datos_entrenamiento)
Residuals:
Min 1Q Median 3Q Max
-9537905 -2074465 -995819 1059946 102881331
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -32238911 383848 -83.99 <0.0000000000000002 ***
Overall 520191 5766 90.22 <0.0000000000000002 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4492000 on 12567 degrees of freedom
Multiple R-squared: 0.3931, Adjusted R-squared: 0.393
F-statistic: 8139 on 1 and 12567 DF, p-value: < 0.00000000000000022
par(mfrow = c(2,2))
plot(modelo)
ggplot(data = datos_esp, mapping = aes(x = Overall, y = Valor)) +
geom_point(color = "firebrick", size = 2) +
labs(title = 'Valor ~ Overall', x = 'Overall') +
geom_smooth(method = "lm", se = FALSE, color = "black") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5))
paste("Valor de a = ", modelo$coefficients[1])
[1] "Valor de a = -32238911.0242445"
paste("Valor de b = ", modelo$coefficients[2])
[1] "Valor de b = 520191.088024262"
predicciones <- predict(modelo, newdata = datos_validacion)
9.1 - Agregar información del modelo a dataframe:
datos_validacion <- mutate(datos_validacion, prediccion = predicciones)
head(datos_validacion, 10)
Overall Value Valor prediccion
1 91 \20072M 72000000 15098478
2 91 \20051M 51000000 15098478
3 90 \20068M 68000000 14578287
4 89 \20089M 89000000 14058096
5 89 \20060M 60000000 14058096
6 89 \20038M 38000000 14058096
7 89 \20027M 27000000 14058096
8 88 \20069.5M 69500000 13537905
9 88 \20062M 62000000 13537905
10 88 \20073.5M 73500000 13537905
tail(datos_validacion, 10)
Overall Value Valor prediccion
5377 48 \20020K 2000 -7269739
5378 48 \20060K 6000 -7269739
5379 48 \20040K 4000 -7269739
5380 47 \20040K 4000 -7789930
5381 47 \20050K 5000 -7789930
5382 47 \20060K 6000 -7789930
5383 47 \20060K 6000 -7789930
5384 47 \20070K 7000 -7789930
5385 47 \20060K 6000 -7789930
5386 47 \20060K 6000 -7789930