-
Notifications
You must be signed in to change notification settings - Fork 0
/
ejercicio-de-modelaje.qmd
174 lines (131 loc) · 3.14 KB
/
ejercicio-de-modelaje.qmd
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
---
title: "Ejercicio de Modelaje"
format: html
editor: visual
---
## Conectar los datos
```{r}
if(!require(googlesheets4)){
install.packages("googlesheets4")
require(googlesheets4)
}
library(googledrive)
library(magick)
my_files <- dir("R",full.names = TRUE)
for(my_file in my_files){
source(my_file)
}
gs4_deauth()
my_url <- "https://docs.google.com/spreadsheets/d/1SF5DDTEfbQFSNjvYKQnsQKdsBdE7oJeTywCk92pB7f8/edit?usp=sharing"
my_url2 <- 'https://docs.google.com/spreadsheets/d/1SF5DDTEfbQFSNjvYKQnsQKdsBdE7oJeTywCk92pB7f8/edit#gid=1964221805'
my_data <- googlesheets4::read_sheet(my_url2)
```
## Acceder los links
De prueba:
```{r}
dir.create("tmp2")
setwd("tmp2")
tmp_file <- drive_download(my_data$URL[2],overwrite = TRUE)
my_image <- image_read(path =tmp_file$local_path)
my_df <- to_RGB_df(my_image)
my_df %>%
summarize(
Red = mean(R),
Green = mean(G),
Blue = mean(B),
Red_sd = sd(R),
Green_sd = sd(G),
Blue_sd = sd(B),
)
```
```{r}
setwd("tmp2")
my_data <- my_data %>%
filter(!is.na(URL))
my_new_data <- data.frame()
index <- 1
for(URL in my_data$URL){
index <- index +1
cat(index, " Analizando: ", URL, "\n")
tmp_file <- drive_download(URL, overwrite = TRUE)
my_image <- image_read(path =tmp_file$local_path)
my_df <- to_RGB_df(my_image)
my_df %>%
summarize(
Red = mean(R),
Green = mean(G),
Blue = mean(B),
Red_sd = sd(R),
Green_sd = sd(G),
Blue_sd = sd(B),
) -> tmp
my_new_data <- rbind(my_new_data, tmp)
}
```
```{r}
my_data2 <- cbind(my_data,my_new_data)
```
```{r}
my_data2
```
# Hacer backup
```{r}
write_rds(my_data2,"./data/fotos_con_analysis_de_colores.rds")
```
# Modelar.
```{r}
my_data2 %>%
mutate(Cromatica = as.factor(Cromatica)) %>%
glm(Cromatica ~ (Red + Green + Blue)^2,family = "binomial", data=.)-> my_model1
```
```{r}
my_data2 %>%
mutate(Cromatica = as.factor(Cromatica)) %>%
glm(Cromatica ~ Red + Green + Blue,family = "binomial", data=.)-> my_model2
summary(my_model2)
```
```{r}
predict(my_model2,type = "response") -> porcentajes
```
```{r}
my_data2$prediccion <- porcentajes
```
# Evaluar
```{r}
my_data2 <- my_data2 %>%
mutate(pred_dura = ifelse(prediccion>.5, "Frío", "Cálido"))
```
```{r}
sum(my_data2$Cromatica == my_data2$pred_dura)/nrow(my_data2)
```
```{r}
my_data3 <- my_data2
predict(my_model1,type = "response") -> porcentajes
my_data3$prediccion <- porcentajes
my_data3 <- my_data3 %>%
mutate(pred_dura = ifelse(prediccion>.5, "Frío", "Cálido"))
sum(my_data3$Cromatica == my_data3$pred_dura)/nrow(my_data3)
```
## Mas interacciones
```{r}
my_data4 <- my_data2
my_data4 %>%
mutate(Cromatica = as.factor(Cromatica)) %>%
glm(Cromatica ~ (Red + Green + Blue)^3,family = "binomial", data=.)-> my_model4
summary(my_model4)
```
```{r}
predict(my_model4,type = "response") -> porcentajes
my_data4$prediccion <- porcentajes
my_data4 <- my_data4 %>%
mutate(pred_dura = ifelse(prediccion>.5, "Frío", "Cálido"))
sum(my_data4$Cromatica == my_data4$pred_dura)/nrow(my_data4)
```
```{r}
coef(my_model4)
```
\$\$
\hat{Y} \sim R + G + B + \epsilon \$\$
\$\$ \hat{Y} = \[Calido\|Frio\] \$\$
\$\$
Calido\|Frio \sim R + G + B \$\$