getwd()
## [1] "C:/Users/user/Desktop"
setwd("C:/abc")
getwd()
## [1] "C:/abc"
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
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(caret)
## 필요한 패키지를 로딩중입니다: ggplot2
## 필요한 패키지를 로딩중입니다: lattice
ls()
## character(0)
rm(list=ls())
ls()
## character(0)
data(iris)
library(caret)
idx<-createDataPartition(iris$Species,p=0.8)
table(iris[idx$Resample1,"Species"])
##
## setosa versicolor virginica
## 40 40 40
table(iris[-idx$Resample1,"Species"])
##
## setosa versicolor virginica
## 10 10 10
idx<-createDataPartition(iris$Species,p=0.8,list=FALSE)
train<-iris[idx,]
test<-iris[-idx,]
nrow(train)
## [1] 120
nrow(test)
## [1] 30
library(caret)
set.seed(2)
data(iris)
idx<-createDataPartition(iris$Species,p=0.6,list=FALSE)
train<-iris[idx,]
valid_test<-iris[-idx,]
idx2<-createDataPartition(valid_test$Species,p=0.5,list=FALSE)
valid<-valid_test[idx2,]
test<-valid_test[-idx2,]
nrow(train)/nrow(iris)
## [1] 0.6
nrow(valid)/nrow(iris)
## [1] 0.2
nrow(test)/nrow(iris)
## [1] 0.2
library(caret)
data(iris)
zs_iris<-preProcess(iris,method=c("center","scale"))
zs_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - centered (4)
## - ignored (1)
## - scaled (4)
zs_iris_p<-predict(zs_iris,iris)
library(psych)
##
## 다음의 패키지를 부착합니다: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
describe(zs_iris_p[,1:4])
## vars n mean sd median trimmed mad min max range skew
## Sepal.Length 1 150 0 1 -0.05 -0.04 1.25 -1.86 2.48 4.35 0.31
## Sepal.Width 2 150 0 1 -0.13 -0.03 1.02 -2.43 3.08 5.51 0.31
## Petal.Length 3 150 0 1 0.34 0.00 1.05 -1.56 1.78 3.34 -0.27
## Petal.Width 4 150 0 1 0.13 -0.02 1.36 -1.44 1.71 3.15 -0.10
## kurtosis se
## Sepal.Length -0.61 0.08
## Sepal.Width 0.14 0.08
## Petal.Length -1.42 0.08
## Petal.Width -1.36 0.08
library(caret)
data(iris)
sca_iris<-preProcess(iris,method="range")
sca_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - ignored (1)
## - re-scaling to [0, 1] (4)
sca_iris_p<-predict(sca_iris,iris)
head(sca_iris_p)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 0.22222222 0.6250000 0.06779661 0.04166667 setosa
## 2 0.16666667 0.4166667 0.06779661 0.04166667 setosa
## 3 0.11111111 0.5000000 0.05084746 0.04166667 setosa
## 4 0.08333333 0.4583333 0.08474576 0.04166667 setosa
## 5 0.19444444 0.6666667 0.06779661 0.04166667 setosa
## 6 0.30555556 0.7916667 0.11864407 0.12500000 setosa
library(psych)
describe(sca_iris_p)
## vars n mean sd median trimmed mad min max range skew
## Sepal.Length 1 150 0.43 0.23 0.42 0.42 0.29 0 1 1 0.31
## Sepal.Width 2 150 0.44 0.18 0.42 0.43 0.19 0 1 1 0.31
## Petal.Length 3 150 0.47 0.30 0.57 0.47 0.31 0 1 1 -0.27
## Petal.Width 4 150 0.46 0.32 0.50 0.45 0.43 0 1 1 -0.10
## Species* 5 150 2.00 0.82 2.00 2.00 1.48 1 3 2 0.00
## kurtosis se
## Sepal.Length -0.61 0.02
## Sepal.Width 0.14 0.01
## Petal.Length -1.42 0.02
## Petal.Width -1.36 0.03
## Species* -1.52 0.07
library(caret)
data(iris)
bc_iris=preProcess(iris,method="BoxCox")
bc_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - Box-Cox transformation (4)
## - ignored (1)
##
## Lambda estimates for Box-Cox transformation:
## -0.1, 0.3, 0.9, 0.6
bc_iris_t=predict(bc_iris,iris)
head(bc_iris_t)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 1.629241 1.520660 1.4 -1.0321154 setosa
## 2 1.589235 1.301297 1.4 -1.0321154 setosa
## 3 1.547563 1.391905 1.3 -1.0321154 setosa
## 4 1.526056 1.347113 1.5 -1.0321154 setosa
## 5 1.609438 1.561856 1.4 -1.0321154 setosa
## 6 1.686399 1.680826 1.7 -0.7048667 setosa
par(mfrow=c(1,2))
plot(density(x=iris$Sepal.Width))
plot(density(x=bc_iris_t$Sepal.Width))

library(caret)
data(iris)
pca_iris<-preProcess(iris,method="pca")
pca_iris
## Created from 150 samples and 5 variables
##
## Pre-processing:
## - centered (4)
## - ignored (1)
## - principal component signal extraction (4)
## - scaled (4)
##
## PCA needed 2 components to capture 95 percent of the variance
pca_iris_p<-predict(pca_iris,iris)
head(pca_iris_p)
## Species PC1 PC2
## 1 setosa -2.257141 -0.4784238
## 2 setosa -2.074013 0.6718827
## 3 setosa -2.356335 0.3407664
## 4 setosa -2.291707 0.5953999
## 5 setosa -2.381863 -0.6446757
## 6 setosa -2.068701 -1.4842053
library(earth)
## 필요한 패키지를 로딩중입니다: Formula
## 필요한 패키지를 로딩중입니다: plotmo
## 필요한 패키지를 로딩중입니다: plotrix
##
## 다음의 패키지를 부착합니다: 'plotrix'
## The following object is masked from 'package:psych':
##
## rescale
## 필요한 패키지를 로딩중입니다: TeachingDemos
data(etitanic)
library(dplyr)
glimpse(etitanic)
## Rows: 1,046
## Columns: 6
## $ pclass <fct> 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1st, 1…
## $ survived <int> 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1…
## $ sex <fct> female, male, female, male, female, male, female, male, femal…
## $ age <dbl> 29.0000, 0.9167, 2.0000, 30.0000, 25.0000, 48.0000, 63.0000, …
## $ sibsp <int> 0, 1, 1, 1, 1, 0, 1, 0, 2, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1…
## $ parch <int> 0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1…
dummies<-dummyVars(survived~.,data=etitanic)
dummies
## Dummy Variable Object
##
## Formula: survived ~ .
## 6 variables, 2 factors
## Variables and levels will be separated by '.'
## A less than full rank encoding is used
head(predict(dummies,newdata=etitanic))
## pclass.1st pclass.2nd pclass.3rd sex.female sex.male age sibsp parch
## 1 1 0 0 1 0 29.0000 0 0
## 2 1 0 0 0 1 0.9167 1 2
## 3 1 0 0 1 0 2.0000 1 2
## 4 1 0 0 0 1 30.0000 1 2
## 5 1 0 0 1 0 25.0000 1 2
## 6 1 0 0 0 1 48.0000 0 0
data(airquality)
colSums(is.na(airquality))
## Ozone Solar.R Wind Temp Month Day
## 37 7 0 0 0 0
summary(airquality)
## Ozone Solar.R Wind Temp
## Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00
## 1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00
## Median : 31.50 Median :205.0 Median : 9.700 Median :79.00
## Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88
## 3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00
## Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00
## NA's :37 NA's :7
## Month Day
## Min. :5.000 Min. : 1.0
## 1st Qu.:6.000 1st Qu.: 8.0
## Median :7.000 Median :16.0
## Mean :6.993 Mean :15.8
## 3rd Qu.:8.000 3rd Qu.:23.0
## Max. :9.000 Max. :31.0
##
library(RANN)
head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
knn_im<-preProcess(airquality[1:4],method="knnImpute")
head(predict(knn_im,airquality))
## Ozone Solar.R Wind Temp Month Day
## 1 -0.03423409 0.04517615 -0.7259482 -1.1497140 5 1
## 2 -0.18580489 -0.75430487 -0.5556388 -0.6214670 5 2
## 3 -0.91334473 -0.41008388 0.7500660 -0.4101682 5 3
## 4 -0.73145977 1.41095624 0.4378323 -1.6779609 5 4
## 5 -0.69508278 -0.92308420 1.2326091 -2.3118573 5 5
## 6 -0.42831817 -0.28349938 1.4029185 -1.2553634 5 6
knn_im_p<-predict(knn_im,airquality)
colSums(is.na(knn_im_p))
## Ozone Solar.R Wind Temp Month Day
## 0 0 0 0 0 0
describe(knn_im_p)
## vars n mean sd median trimmed mad min max range skew
## Ozone 1 153 -0.04 0.92 -0.31 -0.16 0.72 -1.25 3.82 5.06 1.30
## Solar.R 2 153 -0.01 0.99 0.17 0.03 1.05 -1.99 1.64 3.63 -0.39
## Wind 3 153 0.00 1.00 -0.07 -0.02 0.97 -2.34 3.05 5.39 0.34
## Temp 4 153 0.00 1.00 0.12 0.04 0.94 -2.31 2.02 4.33 -0.37
## Month 5 153 6.99 1.42 7.00 6.99 1.48 5.00 9.00 4.00 0.00
## Day 6 153 15.80 8.86 16.00 15.80 11.86 1.00 31.00 30.00 0.00
## kurtosis se
## Ozone 1.62 0.07
## Solar.R -0.99 0.08
## Wind 0.03 0.08
## Temp -0.46 0.08
## Month -1.32 0.11
## Day -1.22 0.72
median_im<-preProcess(airquality[1:4],method="medianImpute")
head(predict(median_im,airquality))
## Ozone Solar.R Wind Temp Month Day
## 1 41.0 190 7.4 67 5 1
## 2 36.0 118 8.0 72 5 2
## 3 12.0 149 12.6 74 5 3
## 4 18.0 313 11.5 62 5 4
## 5 31.5 205 14.3 56 5 5
## 6 28.0 205 14.9 66 5 6