갈루아의 반서재

Geom(geometric object)은 데이터를 보여주는데 사용되는 객체의 유형으로, gplot 은 산포도에 다양한 형태의 geom 을 표현할 수 있다.이 외에도 "histogram", "freqpoly", "density", "bar" 등 다양한 객체가 존재한다.



geom

설명 

"point"

산포도에 사용. qplot() 사용시 기본값 

"smooth"

데이터의 추세선을 나타내고, 표준오차의 정도를 나타내는 데 사용

"boxplot"

상자그림, 상자수염도, 분포를 요약해서 나타내는 데 사용 

"path", "line"

데이터 포인트 사이에 선으로 연결. line 은 왼쪽에서 오른쪽으로 그려지는 반면, path 는 어느 방향으로든지 가능하다. 

 


1. 추세선 그리기

 

무수한 데이터 포인트로 이루어진 산포도로는 그 경향을 정확히 읽어내기가 어렵다. 

이런 경우, 추세선을 통해 그 경향을 읽어낼 수 있다. 

앞에서 보았듯이 추세선은 gemo="smooth" 를 통해 표현할 수 있으며, 기존 산포도와 중첩해서 나타낼려면 c() 을 이용하면 된다. 

이 경우 c() 안에 정렬된 순서로 geoms 이 레이어처럼 쌓이게 된다.

> qplot(carat, price, data = dsmall, geom = c("point", "smooth"))

geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

 

 

 

> qplot(carat, price, data = diamonds, geom = c("point", "smooth"))

geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.

 

 

 


2.  span 파라메터

 

span 파라메터를 이용하여 추세선의 꾸불꾸불한 정도를 설정할 수 있다.

span 파라메터는 0과 1사이의 범위를 가지며, 0 은 '엄청나게 꾸불꾸불한' 정도를 그리고 1 '별로 꾸불꾸불하지 않은' 정도를 나타낸다.

 

>qplot(carat, price, data = dsmall, geom = c("point", "smooth"),span = 0.2)

 

 

>qplot(carat, price, data = dsmall, geom = c("point", "smooth"),span = 1)

  

 

 

 

3. Generalised additive model(gam, 일반화가법모형)

mgcv 라이브러리를 이용해서 gam 메소드를 아용할 수 있다.


> library("mgcv", lib.loc="~/R/win-library/3.1")

> qplot(carat,price,data=dsmall,geom=c("point","smooth"), method="gam",formula=y~s(x))



대용량 데이터(1,000개 이상의 포인트)에서는 formula = y~s(x, bs="cs") 를 사용한다.


> qplot(carat,price,data=dsmall,geom=c("point","smooth"), method="gam",formula=y~s(x, bs="cs"))


4. Linear model (lm, 선형모델)

기본값은 데이터에 대해 직선이며, 2개의 다항식을 사용하기 위해서는 formula = y ~ poly(x, 2) 을 이용하거나, splines 패키지를 로딩하여 formula = y ~ ns(x,2)와 같이 natural spline 을 이용한다.



> library(splines)

> qplot(carat, price, data = dsmall, geom=c("point", "smooth"), method = "lm")



> qplot(carat, price, data = dsmall, geom=c("point", "smooth"), method = "lm", formula=y ~ ns(x,3))

두 번째 파라메터는 자유도의 정도를 나타내는 것으로 숫자가 커질수록 더 꾸불꾸불한 물결모양의 커브를 만들어낸다.


마지막으로 rlm 메소드는 lm 과 유사하지만, 이상값으로부터 영향을 적게 받도록 탄탄한 알고리즘을 채택하고 있다. MASS 패키지에 포함되어 있다.