-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_Do not change.Rmd
190 lines (155 loc) · 7.52 KB
/
project_Do not change.Rmd
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
---
title: "project II"
output: html_document
---
```{r,echo=FALSE, results='hide',message=FALSE,warning=FALSE}
###loading libraries
suppressWarnings(library(dplyr))
suppressWarnings(library(purrr))
suppressWarnings(library(knitr))
suppressWarnings(library(grid))
suppressWarnings(library(ggplot2))
```
```{r,echo=FALSE, results='hide',message=FALSE,warning=FALSE}
###loading dataset
load("paintings_train.Rdata")
train <- paintings_train
```
```{r,echo=FALSE, results='hide',message=FALSE,warning=FALSE}
###select variables
#Professor's recoding
train <- train %>%
mutate(shape_recode = ifelse(Shape == "", "Not Available",
ifelse(Shape == "ovale", "oval",
ifelse(Shape == "ronde", "round",
ifelse(Shape == "octogon", "octagon", Shape)))))
#recode
train <- train %>%
mutate(mat_recode = ifelse(mat %in% c("a", "bc", "c","br"), "metal",
ifelse(mat %in% c("al", "ar", "m"), "stone",
ifelse(mat %in% c("co", "bt", "t","h","ta"), "canvas",
ifelse(mat %in% c("p", "ca"), "paper",
ifelse(mat %in% c("b"), "wood",
ifelse(mat %in% c("o", "e","v","mi","pa","g"), "other",
ifelse(mat %in% c("n/a", ""), "uncertain", NA))))))))
train <- train%>%
mutate(fig_mention = ifelse(nfigures ==0, "no figures", "figures"))
train <- train%>%
mutate(artist_living_notliving = ifelse(artistliving==0, "not living", "living"))
train <- train%>%
mutate(history_nohistory = ifelse(history==0, "no history", "history"))
train <- train%>%
mutate(mytho_nomytho = ifelse(mytho==0, "no mytho", "mytho"))
train <- train%>%
mutate(finished_nofinished = ifelse(finished==0, "no finished", "finished"))
train <- train%>%
mutate(LF = ifelse(lrgfont==0, "no LF", "LF"))
#change price into numeric
train$price <- as.numeric(gsub(",","",train$price))
#create a new variable "famous_author"
author_price <- train %>%
group_by(authorstandard) %>%
summarise(mean_price = mean(price)) %>%
ungroup() %>%
arrange(desc(mean_price))
train <- train %>%
mutate(famous_author = ifelse(authorstandard %in%
author_price$authorstandard[which(author_price$mean_price >= 3000)],1,0))
#remove completely descriptive (thus irrelevant) variables
newTrain <- train[,!names(train) %in% c("sale","lot","position","logprice","subject",
"authorstyle","authorstandard","author",
"winningbidder",
"Interm","Height_in","Width_in","Surface_Rect",
"Diam_in","Surface_Rnd","material","mat",
"lands_sc","lands_elem","lands_figs","lands_ment")]
#NA
which(apply(newTrain,2,anyNA) == TRUE)
#remove NA
newTrain <- newTrain[complete.cases(newTrain$Surface),]
#change all character strings to factors
character_vars <- lapply(newTrain, class) == "character"
newTrain[, character_vars] <- lapply(newTrain[, character_vars], as.factor)
#change the level of factor
levels(newTrain$type_intermed)[levels(newTrain$type_intermed)==""] <- "NONE"
levels(newTrain$winningbiddertype)[levels(newTrain$winningbiddertype)==""] <- "NONE"
levels(newTrain$endbuyer)[levels(newTrain$endbuyer)==""] <- "NONE"
levels(newTrain$materialCat)[levels(newTrain$materialCat)==""] <- "NONE"
levels(newTrain$school_pntg)[levels(newTrain$school_pntg)=="S"] <- "OTHER"
levels(newTrain$winningbiddertype)[levels(newTrain$winningbiddertype)=="EBC"] <- "OTHER"
```
```{r,echo=FALSE, results='hide',message=FALSE,warning=FALSE}
###testing dataset
load("paintings_test.Rdata")
test <- paintings_test
#recode
test <- test %>%
mutate(mat_recode = ifelse(mat %in% c("a", "bc", "c","br"), "metal",
ifelse(mat %in% c("al", "ar", "m"), "stone",
ifelse(mat %in% c("co", "bt", "t","h","ta"), "canvas",
ifelse(mat %in% c("p", "ca"), "paper",
ifelse(mat %in% c("b"), "wood",
ifelse(mat %in% c("o", "e","v","mi","pa","g"), "other",
ifelse(mat %in% c("n/a", ""), "uncertain", NA))))))))
test <- test%>%
mutate(fig_mention = ifelse(nfigures ==0, "no figures", "figures"))
test <- test%>%
mutate(artist_living_notliving = ifelse(artistliving==0, "not living", "living"))
test <- test%>%
mutate(history_nohistory = ifelse(history==0, "no history", "history"))
test <- test%>%
mutate(mytho_nomytho = ifelse(mytho==0, "no mytho", "mytho"))
test <- test%>%
mutate(finished_nofinished = ifelse(finished==0, "no finished", "finished"))
test <- test%>%
mutate(LF = ifelse(lrgfont==0, "no LF", "LF"))
#create a new variable "famous_author"
author_price <- train %>%
group_by(authorstandard) %>%
summarise(mean_price = mean(price)) %>%
ungroup() %>%
arrange(desc(mean_price))
test <- test %>%
mutate(famous_author = ifelse(authorstandard %in%
author_price$authorstandard[which(author_price$mean_price >= 3000)],1,0))
#Professor's recoding
test <- test %>%
mutate(shape_recode = ifelse(Shape == "", "Not Available",
ifelse(Shape == "ovale", "oval",
ifelse(Shape == "ronde", "round",
ifelse(Shape == "octogon", "octagon", Shape)))))
newTest <- test[,!names(test) %in% c("sale","lot","position","logprice","subject",
"authorstyle","authorstandard","author",
"winningbidder", "price",
"Interm","Height_in","Width_in","Surface_Rect",
"Diam_in","Surface_Rnd","material","mat",
"lands_sc","lands_elem","lands_figs","lands_ment")]
#change all character strings to factors
character_vars <- lapply(newTest, class) == "character"
newTest[, character_vars] <- lapply(newTest[, character_vars], as.factor)
#change the level of factors
levels(newTest$type_intermed)[levels(newTest$type_intermed)==""] <- "NONE"
levels(newTest$winningbiddertype)[levels(newTest$winningbiddertype)==""] <- "NONE"
levels(newTest$endbuyer)[levels(newTest$endbuyer)==""] <- "NONE"
levels(newTest$materialCat)[levels(newTest$materialCat)==""] <- "NONE"
levels(newTest$school_pntg)[levels(newTest$school_pntg)=="A"] <- "OTHER"
levels(newTest$school_pntg)[levels(newTest$school_pntg)=="G"] <- "OTHER"
levels(newTest$winningbiddertype)[levels(newTest$winningbiddertype)=="EBC"] <- "OTHER"
levels(newTest$winningbiddertype)[levels(newTest$winningbiddertype)=="BB"] <- "OTHER"
levels(newTest$winningbiddertype)[levels(newTest$winningbiddertype)=="ED"] <- "OTHER"
#simple method to predict the NA in surface
newTest$Surface[which(is.na(newTest$Surface)==TRUE)] <- mean(newTest$Surface, na.rm = TRUE)
```
```{r,echo=FALSE, results='hide',message=FALSE,warning=FALSE}
###Testing Push
m <- lm(formula = log(price) ~ dealer + year + school_pntg + diff_origin +
artistliving + winningbiddertype + Surface + engraved +
prevcoll + paired + finished + lrgfont + othgenre +
discauth + winningbiddertype:prevcoll +
Surface:mat_recode + famous_author:Surface, data = newTrain)
```
```{r,echo=FALSE,message=FALSE,results = 'hide',warning=FALSE}
predictions = as.data.frame(
exp(predict(m, newdata=newTest,
interval = "pred")))
save(predictions, file="predict-test.Rdata")
```