프로그램 소스 및 결과

# R을 이용한 통계 프로그래밍 기초

# 5 일변량 데이터와 기술통계량

# 5.1 범주형 데이터

# 5.1.1 분할표

res = c("y", "n", "y", "y", "y", "n", "n", "y", "y", "y")

table(res) # 분할표 만들기

# 5.1.2 막대그래프

jpeg("05_01.jpg")
barplot(table(res), xlab = "response", ylab = "frequency") # 막대그래프
dev.off()

jpeg("05_02.jpg")
barplot(table(res), xlab = "response", ylab = "frequency", horiz = T) # 수평 막대그래프
dev.off()

# 5.1.3 파이그림

jpeg("05_03.jpg")
pie(table(res), main = "response") # 파이 그래프
dev.off()

# 5.1.4 점그림

jpeg("05_04.jpg")
dotchart(table(res), main = "dotchart") # 점 그래프
dev.off()

sales = c(50, 39, 47, 45) # 벡터 생성
names(sales) = c("Amy", "John", "Jack", "Lisa") # 벡터 데이터에 이름붙이기
jpeg("05_05.jpg")
dotchart(sales, xlab = "Amount of sales", main = "dotchart") # 점 그래프 그리기
dev.off()


# 5.2 숫자형 데이터

# 5.2.1 줄기-잎-그림

x = c(45,86,34,98,67,78,56,45,85,75,64,75,75,75,58,45,83,74)
stem(x) # stem-and-leaf plot

stem(x, scale = 2) # stem-and-leaf plot

# 5.2.2 상자그림

jpeg("05_08.jpg")
op = par(mfrow = c(1,2))
boxplot(x, main = "Box plot", sub = "basketball game score") # 상자그림(boxplot)
boxplot(x, horizontal = TRUE, main = "Box plot", sub = "basketball game score") # 상자그림(boxplot)
par(op)
dev.off()

# 5.2.3 히스토그램

jpeg("05_09.jpg")
hist(x
, prob = TRUE # 확률로 표시
, main = "Histogram of score with density line", # 큰 제목
, sub = "basketball game score") # 작은 제목
lines(density(x)) # 라인 추가
dev.off()

# 5.2.4 중심경향 측도 : 평균, 중앙값

mean(x) # 평균
median(x) # 중앙값

# 5.2.5 퍼짐경향 측도, 분산, 표준편차, 사분위수범위

var(x) # 분산
sd(x) # 표준편차
IQR(x) # 사분위수 범위(inter-quantile range)
range(x) # 범위
quantile(x) # 사분위수
quantile(x, c(0.1, 0.25, 0.5, 0.75, 0.9)) # 지정 사분위수
summary(x) # 기술통계 요약

# 5.2.6 표준화점수

y = c(2, 5, 7, 9, 3)
scale(y) # scale 함수를 이용한 표준화
(y - mean(y))/sd(y) # 평균과 표준편차를 이용한 표준화
round(scale(y), digits =2) # 표준화 값을 소수 2째자리까지 표시

# 5.2.7 신뢰구간

install.packages("plotrix", dependencies = TRUE)

library(plotrix)
m = 10
meany = rep(0, m)
sey = rep(0, m)
n = 30
for(i in 1:m){
y = rnorm(n)
meany[i] = mean(y)
sey[i] = sd(y)/sqrt(n)
}

jpeg("05_10.jpg")
op = par(mfrow = c(1,2))
plotCI(1:10,
meany,
sey,
main = "Confidence Interval")
abline(h = 0)

plotCI(meany, 1:10, 2*sey, pch = 21, err ="x",
main = "CI with horizontal bars")
abline(v = 0)
par(op)
dev.off()

jpeg("05_11.jpg")
plotCI(1:10,
meany,
sey,
main = "Confidence Interval",
lwd = 2,
col = "red",
scol = "blue")
abline(h = 0)
dev.off()

프로그램 결과


R version 2.8.1 (2008-12-22)
Copyright (C) 2008 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R은 free 소프트웨어이고, [완전하게 무보증]입니다.
일정한 조건에 따르면, 자유롭게 이것을 재배포할수가 있습니다.
배포 조건의 상세한것에 대해서는 'license()' 또는 'licence()' 라고 입력해주십시오

R는 많은 공헌자에의한 공동 프로젝트입니다
더 자세한것에 대해서는 'contributors()'라고 입력해 주십시오.
또는, R나 R의 패키지를 출판물로 인용할때의 형식에 대해서는
'citation()'라고 입력해주십시오
'demo()'라고 입력하면, demos를 볼수가 있습니다.
'help()'라고 한다면, on-line help가 나옵니다.
'help.start()'로 HTML 브라우저에 의한 help가 보여집니다
'q()'라고 입력하면 R를 종료합니다
> # R을 이용한 통계 프로그래밍 기초
>
> # 5 일변량 데이터와 기술통계량
>
> # 5.1 범주형 데이터
>
> # 5.1.1 분할표
>
> res = c("y", "n", "y", "y", "y", "n", "n", "y", "y", "y")
>
> table(res) # 분할표 만들기
res
n y
3 7
>
> # 5.1.2 막대그래프
>
> jpeg("05_01.jpg")
> barplot(table(res), xlab = "response", ylab = "frequency") # 막대그래프
> dev.off()
null device
1
>



> jpeg("05_02.jpg")
> barplot(table(res), xlab = "response", ylab = "frequency", horiz = T) # 수평 막대그래프
> dev.off()
null device
1
>




> # 5.1.3 파이그림
>
> jpeg("05_03.jpg")
> pie(table(res), main = "response") # 파이 그래프
> dev.off()
null device
1
>




> # 5.1.4 점그림
>
> jpeg("05_04.jpg")
> dotchart(table(res), main = "dotchart") # 점 그래프
> dev.off()
null device
1
>




> sales = c(50, 39, 47, 45) # 벡터 생성
> names(sales) = c("Amy", "John", "Jack", "Lisa") # 벡터 데이터에 이름붙이기
> jpeg("05_05.jpg")
> dotchart(sales, xlab = "Amount of sales", main = "dotchart") # 점 그래프 그리기
> dev.off()
null device
1
>




>
> # 5.2 숫자형 데이터
>
> # 5.2.1 줄기-잎-그림
>
> x = c(45,86,34,98,67,78,56,45,85,75,64,75,75,75,58,45,83,74)
> stem(x) # stem-and-leaf plot

The decimal point is 1 digit(s) to the right of the |

2 | 4
4 | 55568
6 | 47455558
8 | 3568

>
> stem(x, scale = 2) # stem-and-leaf plot

The decimal point is 1 digit(s) to the right of the |

3 | 4
4 | 555
5 | 68
6 | 47
7 | 455558
8 | 356
9 | 8

>
> # 5.2.2 상자그림
>
> jpeg("05_08.jpg")
> op = par(mfrow = c(1,2))
> boxplot(x, main = "Box plot", sub = "basketball game score") # 상자그림(boxplot)
> boxplot(x, horizontal = TRUE, main = "Box plot", sub = "basketball game score") # 상자그림(boxplot)
> par(op)
> dev.off()
null device
1
>




> # 5.2.3 히스토그램
>
> jpeg("05_09.jpg")
> hist(x
+ , prob = TRUE # 확률로 표시
+ , main = "Histogram of score with density line", # 큰 제목
+ , sub = "basketball game score") # 작은 제목
> lines(density(x)) # 라인 추가
> dev.off()
null device
1
>




> # 5.2.4 중심경향 측도 : 평균, 중앙값
>
> mean(x) # 평균
[1] 67.66667
> median(x) # 중앙값
[1] 74.5
>
> # 5.2.5 퍼짐경향 측도, 분산, 표준편차, 사분위수범위
>
> var(x) # 분산
[1] 298.3529
> sd(x) # 표준편차
[1] 17.27290
> IQR(x) # 사분위수 범위(inter-quantile range)
[1] 20.75
> range(x) # 범위
[1] 34 98
> quantile(x) # 사분위수
0% 25% 50% 75% 100%
34.00 56.50 74.50 77.25 98.00
> quantile(x, c(0.1, 0.25, 0.5, 0.75, 0.9)) # 지정 사분위수
10% 25% 50% 75% 90%
45.00 56.50 74.50 77.25 85.30
> summary(x) # 기술통계 요약
Min. 1st Qu. Median Mean 3rd Qu. Max.
34.00 56.50 74.50 67.67 77.25 98.00
>
> # 5.2.6 표준화점수
>
> y = c(2, 5, 7, 9, 3)
> scale(y) # scale 함수를 이용한 표준화
[,1]
[1,] -1.11748847
[2,] -0.06984303
[3,] 0.62858727
[4,] 1.32701756
[5,] -0.76827333
attr(,"scaled:center")
[1] 5.2
attr(,"scaled:scale")
[1] 2.863564
> (y - mean(y))/sd(y) # 평균과 표준편차를 이용한 표준화
[1] -1.11748847 -0.06984303 0.62858727 1.32701756 -0.76827333
> round(scale(y), digits =2) # 표준화 값을 소수 2째자리까지 표시
[,1]
[1,] -1.12
[2,] -0.07
[3,] 0.63
[4,] 1.33
[5,] -0.77
attr(,"scaled:center")
[1] 5.2
attr(,"scaled:scale")
[1] 2.863564
>
> # 5.2.7 신뢰구간
>
> install.packages("plotrix", dependencies = TRUE)

>
> library(plotrix)
> m = 10
> meany = rep(0, m)
> sey = rep(0, m)
> n = 30
> for(i in 1:m){
+ y = rnorm(n)
+ meany[i] = mean(y)
+ sey[i] = sd(y)/sqrt(n)
+ }
>
> jpeg("05_10.jpg")
> op = par(mfrow = c(1,2))
> plotCI(1:10,
+ meany,
+ sey,
+ main = "Confidence Interval")
> abline(h = 0)
>
> plotCI(meany, 1:10, 2*sey, pch = 21, err ="x",
+ main = "CI with horizontal bars")
> abline(v = 0)
> par(op)
> dev.off()
null device
1
>





> jpeg("05_11.jpg")
> plotCI(1:10,
+ meany,
+ sey,
+ main = "Confidence Interval",
+ lwd = 2,
+ col = "red",
+ scol = "blue")
> abline(h = 0)
> dev.off()
null device
1
>




>
>

+ Recent posts