빅데이터 통계학 LN2

aa <- data.frame(a = c(1,2), b = c("2","d"))
bb <- data.frame(w = 1,d = 2)

#
만들어 놓은 데이터 저장하기
#
확장자명을 반드시 .Rdata 써야한다.
#save(aa,file = "c:/Users/
학습러/Desktop/pr1.rdata")

#
만들어 놓은 데이터 전부 저장하기
#save.image(file = "c:/Users/
학습러/Desktop/pr1.rdata")

#
저장한 파일 불러오기
#load(file = "c:/Users/
학습러/Desktop/pr1.rdata")

#
만들어 놓은 object 리스트 반환
ls()

## [1] "aa" "bb"

#data 구조 알기
data_2 <- read.csv("usedcars.csv", stringsAsFactors = F)
head(data_2,n = 10) #
위에서 10

##    year model price mileage  color transmission
## 1  2011   SEL 21992    7413 Yellow         AUTO
## 2  2011   SEL 20995   10926   Gray         AUTO
## 3  2011   SEL 19995    7351 Silver         AUTO
## 4  2011   SEL 17809   11613   Gray         AUTO
## 5  2012    SE 17500    8367  White         AUTO
## 6  2010   SEL 17495   25125 Silver         AUTO
## 7  2011   SEL 17000   27393   Blue         AUTO
## 8  2010   SEL 16995   21026 Silver         AUTO
## 9  2011   SES 16995   32655 Silver         AUTO
## 10 2010   SES 16995   36116 Silver         AUTO

tail(data_2) # n 조건 없으면 5

##     year model price mileage  color transmission
## 145 2004   SES  6950  119720  Black         AUTO
## 146 2006   SES  6200   95000 Silver         AUTO
## 147 2002    SE  5995   87003    Red         AUTO
## 148 2000    SE  5980   96841    Red         AUTO
## 149 2001    SE  4899  151479 Yellow         AUTO
## 150 2000    SE  3800  109259    Red         AUTO

summary(data_2$price)

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    3800   10995   13592   12962   14904   21992

range(data_2$price) #최소값 최대값

## [1]  3800 21992

diff(range(data_2$price)) # 최대값 - 최소값

## [1] 18192

qt1 <- quantile(data_2$price, probs = 0.25) ;qt1

##   25%
## 10995

quantile(data_2$price, probs = seq(0,1,0.1))

##      0%     10%     20%     30%     40%     50%     60%     70%     80%
##  3800.0  8431.9 10759.4 11982.8 12993.8 13591.5 13992.0 14496.2 14999.0
##     90%    100%
## 15999.1 21992.0

IQR(data_2$price)

## [1] 3909.5

# Box plot, Box whisker plot
boxplot(data_2$price, main = "boxplot")


# histogram
hist(data_2$price, main = "histogram",
     xlab = "price", ylab = "freq")


# 계급구간 임의로 지정
hist(data_2$price, breaks = seq(0,25000,1000))


# 범주형 변수 (categorical variables)
# frequency table
table(data_2$model)

##
##  SE SEL SES
##  78  23  49

prop.table(table(data_2$model)) # 비율

##
##        SE       SEL       SES
## 0.5200000 0.1533333 0.3266667

# scatter plot
plot(y = data_2$price, x = data_2$mileage)

#함수형태로 지정 y~x형태임
plot(price~mileage, data= data_2)

# 상관관계 분석 (correlation analysis)

cor(data_2$price, data_2$mileage)

## [1] -0.8061494

cor(data_2[c(1,3,4)]) # 변수에 대한 상관계수

##               year      price    mileage
## year     1.0000000  0.8450041 -0.7603127
## price    0.8450041  1.0000000 -0.8061494
## mileage -0.7603127 -0.8061494  1.0000000

cov(data_2$price, data_2$mileage) # 공분산

## [1] -67918993

# Two-way frequency table (질적변수 두개)
mytab <- table(data_2$model,data_2$transmission) ;mytab

##     
##       AUTO MANUAL
##   SE    63     15
##   SEL   22      1
##   SES   43      6

prop.table(mytab)

##     
##              AUTO      MANUAL
##   SE  0.420000000 0.100000000
##   SEL 0.146666667 0.006666667
##   SES 0.286666667 0.040000000

# data_2$color 값이 in 뒤에 있는 문장에
#
해당하는 색에 대해 논리값 반환
data_2$conservative <- data_2$color %in%
  c("Black","Gray","White","Silver") ;data_2$conservative

##   [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [12]  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
##  [23]  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
##  [34]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [45] FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE
##  [56]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [67] FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE
##  [78]  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE
##  [89]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE
## [100]  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE FALSE  TRUE FALSE  TRUE
## [111] FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE  TRUE
## [122] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE
## [133]  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE  TRUE FALSE
## [144]  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE

table(data_2$conservative)

##
## FALSE  TRUE
##    51    99

table(data_2$model,data_2$conservative)

##     
##       FALSE TRUE
##   SE     27   51
##   SEL     7   16
##   SES    17   32

# Chi-square test ( 질적변수에 대한 독립성 검정)
# gmodels
패키지 사용
#install.packages("gmodels")
#library(gmodels)

#gmodels::CrossTable(data_2$model,
#                    data_2$conservative,prop.r = F,
#                    prop.c = F,prop.t = F,chisq = T)

#
귀무가설은 변수가 무관하다.(독립성)
# p = 0.92591
귀무가설을 기각할 없기 때문에
#
변수가 무관하다.

chisq.test(data_2$model,data_2$conservative)

##
##  Pearson's Chi-squared test
##
## data:  data_2$model and data_2$conservative
## X-squared = 0.15396, df = 2, p-value = 0.9259

# Test whether year and model
# variable are independant or not
chisq.test(data_2$year,data_2$model)

## Warning in chisq.test(data_2$year, data_2$model): Chi-squared approximation
## may be incorrect

##
##  Pearson's Chi-squared test
##
## data:  data_2$year and data_2$model
## X-squared = 43.836, df = 24, p-value = 0.007971

# p-value = 0.007971 variable are not independant


'IT,인터넷 관련 학습 > R언어 학습' 카테고리의 다른 글

빅데이터 통계학 (4)  (0) 2019.04.05
빅데이터 통계학 (3)  (0) 2019.04.05
빅데이터 통계학 (1)  (0) 2019.04.05
R언어 : 다양한 통계차트  (0) 2019.03.02
R언어 : R 통계 관련 함수  (1) 2019.03.02

+ Recent posts