############################################################################################################
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(caret)
## 필요한 패키지를 로딩중입니다: lattice
library(lubridate)
## 
## 다음의 패키지를 부착합니다: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## 다음의 패키지를 부착합니다: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
rm(list=ls())
ls()
## character(0)
getwd()
## [1] "C:/data"
list.files()
##  [1] "6_x_test.csv"  "6_x_train.csv" "6_y_train.csv" "Data1.csv"    
##  [5] "Data1.txt"     "Data1.xls"     "Day1.html"     "Day1.Rmd"     
##  [9] "Day2-2-.html"  "Day2-3-.html"  "Day2(2).Rmd"   "Day2(3).Rmd"  
## [13] "Day2.html"     "Day2.Rmd"      "Day3.html"     "Day3.Rmd"     
## [17] "Day4-1.Rmd"    "Day4.html"     "Day4.Rmd"      "diagnosis.csv"
## [21] "mtcars.csv"    "mypcprotector" "netflix.csv"   "period1.xlsx" 
## [25] "pub.csv"       "rsconnect"     "source data"   "ta1.csv"      
## [29] "X_test.csv"    "X_train.csv"   "y_train.csv"
df <- read.csv("pub.csv")
glimpse(df)
## Rows: 165
## Columns: 10
## $ X          <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ Case       <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
## $ Restaurant <chr> "Daniella Ristorante", "Tello's Ristorante", "Biricchino", …
## $ Price      <int> 43, 32, 34, 41, 54, 52, 34, 34, 39, 44, 45, 47, 52, 35, 47,…
## $ Food       <int> 22, 20, 21, 20, 24, 22, 22, 20, 22, 21, 19, 21, 21, 19, 20,…
## $ Decor      <int> 18, 19, 13, 20, 19, 22, 16, 18, 19, 17, 17, 19, 19, 17, 18,…
## $ Service    <int> 20, 19, 18, 17, 21, 21, 21, 21, 22, 19, 20, 21, 20, 19, 21,…
## $ East       <int> 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ latitude   <dbl> 40.74683, 40.74342, 40.74886, 40.74848, 40.73958, 40.74069,…
## $ longitude  <dbl> -73.99676, -73.99954, -73.99552, -74.00331, -73.99591, -73.…
df <- df[,c(-1,-3)]
glimpse(df)
## Rows: 165
## Columns: 8
## $ Case      <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 1…
## $ Price     <int> 43, 32, 34, 41, 54, 52, 34, 34, 39, 44, 45, 47, 52, 35, 47, …
## $ Food      <int> 22, 20, 21, 20, 24, 22, 22, 20, 22, 21, 19, 21, 21, 19, 20, …
## $ Decor     <int> 18, 19, 13, 20, 19, 22, 16, 18, 19, 17, 17, 19, 19, 17, 18, …
## $ Service   <int> 20, 19, 18, 17, 21, 21, 21, 21, 22, 19, 20, 21, 20, 19, 21, …
## $ East      <int> 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ latitude  <dbl> 40.74683, 40.74342, 40.74886, 40.74848, 40.73958, 40.74069, …
## $ longitude <dbl> -73.99676, -73.99954, -73.99552, -74.00331, -73.99591, -73.9…
colSums(is.na(df))
##      Case     Price      Food     Decor   Service      East  latitude longitude 
##         0         0         0         0         0         0         0         0
set.seed(42)
intrain <- createDataPartition(df$Price,p=0.7,list=FALSE)
train <- df[intrain,]
test <- df[-intrain,]
ctrl <- trainControl(method="cv",number=5)

#1
model_lm <- train(Price~.,data=train,method="lm",trControl=ctrl,preProcess=c("center","scale"))
model_lm
## Linear Regression 
## 
## 118 samples
##   7 predictor
## 
## Pre-processing: centered (7), scaled (7) 
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 95, 94, 93, 96, 94 
## Resampling results:
## 
##   RMSE      Rsquared   MAE     
##   6.269073  0.5690505  4.921196
## 
## Tuning parameter 'intercept' was held constant at a value of TRUE
pred_model_lm <- predict(model_lm,newdata=test)
pred_model_lm
##        6       10       12       13       18       24       39       43 
## 52.08071 42.13860 47.10011 46.97770 56.18551 48.31481 49.90232 54.08572 
##       46       49       51       56       57       58       62       63 
## 35.98103 33.47453 48.33015 47.01512 36.99084 45.84515 37.39916 45.32291 
##       64       70       71       81       83       91       92       96 
## 36.72759 46.81114 52.56939 49.98662 40.89769 58.13121 46.40439 33.01784 
##       97       99      106      108      109      111      117      119 
## 40.32924 35.47943 36.07377 37.54340 35.61776 55.29914 33.21592 35.48760 
##      121      126      132      135      136      140      142      144 
## 50.19298 34.49542 42.85287 33.81839 33.75343 56.60672 58.19419 45.47272 
##      145      148      150      154      159      160      165 
## 52.88847 38.58356 44.54244 45.59940 35.78299 35.53777 31.60464
error <- test$Price-pred_model_lm
sqrt(mean(error^2))
## [1] 5.147235
#2
model <- train(Price~.,data=train,method="rf",trControl=ctrl,preProcess=c("center","scale"))
model
## Random Forest 
## 
## 118 samples
##   7 predictor
## 
## Pre-processing: centered (7), scaled (7) 
## Resampling: Cross-Validated (5 fold) 
## Summary of sample sizes: 94, 94, 94, 94, 96 
## Resampling results across tuning parameters:
## 
##   mtry  RMSE      Rsquared   MAE     
##   2     6.574385  0.5188884  5.039913
##   4     6.629080  0.5157511  5.092619
##   7     6.731398  0.5056378  5.218672
## 
## RMSE was used to select the optimal model using the smallest value.
## The final value used for the model was mtry = 2.
pred_model <- predict(model,newdata=test)
pred_model
##        6       10       12       13       18       24       39       43 
## 46.41737 39.68204 41.29928 42.80728 51.56050 50.38699 52.41663 54.08336 
##       46       49       51       56       57       58       62       63 
## 42.72577 38.77150 48.67069 52.74853 34.98050 48.81370 36.96715 40.01580 
##       64       70       71       81       83       91       92       96 
## 35.14993 48.13780 50.36241 49.51575 37.40315 50.98830 47.75920 38.68747 
##       97       99      106      108      109      111      117      119 
## 37.49858 36.49417 35.34893 35.70466 34.75850 49.79296 33.68013 35.94809 
##      121      126      132      135      136      140      142      144 
## 50.05977 36.09810 43.35673 34.63767 32.88132 50.08267 50.79563 43.27771 
##      145      148      150      154      159      160      165 
## 50.07463 42.29225 44.21993 44.16117 35.51011 32.96617 36.31042
error <- test$Price-pred_model
sqrt(mean(error^2))
## [1] 6.09924
# train 보다 lm 이 표준편차 값이 더 적음

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.