Sunday, April 13, 2014

graphical meaning of the correlation in linear regression

graphical meaning of correlation in linear regression

graphical meaning of correlation in linear regression

First guess, Which does plot have higher correlation?

par(mfrow = c(2, 1))
x1 = rnorm(1000) + seq(0.01, 10, 0.01)
y1 = rnorm(1000, 0, 2) + seq(0.01, 10, 0.01)
plot(x1, y1, xlim = c(-2, 12), ylim = c(-2, 12))
abline(lm(y1 ~ x1))
abline(0, 1, col = "pink")

x2 = rnorm(1000) + seq(0.01, 10, 0.01)
y2 = seq(-1, 0.998, 0.002) + seq(0.001, 1, 0.001)
plot(x2, y2, xlim = c(-2, 12), ylim = c(-2, 12), col = "red")
abline(lm(y2 ~ x2))
abline(0, 1, col = "pink")

plot of chunk unnamed-chunk-1

Answer is the second plot!

par(mfrow = c(2, 1))
plot(x1, y1, main = paste("correlation = ", signif(cor(x1, y1))), xlim = c(-2, 
    12), ylim = c(-2, 12))
abline(lm(y1 ~ x1))
plot(x2, y2, main = paste("correlation = ", signif(cor(x2, y2))), xlim = c(-2, 
    12), ylim = c(-2, 12), col = "red")
abline(lm(y2 ~ x2))

plot of chunk unnamed-chunk-2

Then, How about this?

par(mfrow = c(3, 1))
x3 = seq(-1, 0.998, 0.002) + seq(0.01, 10, 0.01)
y3 = rnorm(1000, 0, 0.01) + seq(0.01, 10, 0.01)
plot(x3, y3, xlim = c(-2, 12), ylim = c(-2, 12), main = paste("correlation =", 
    cor(x3, y3)))
abline(lm(y3 ~ x3))

x4 = seq(-1, 0.998, 0.002) + seq(0.01, 10, 0.01)
y4 = rnorm(1000, 0, 0.01) + seq(0.001, 1, 0.001)
plot(x4, y4, xlim = c(-2, 12), ylim = c(-2, 12), col = "red", main = paste("correlation =", 
    cor(x4, y4)))
abline(lm(y4 ~ x4))

x5 = seq(-1, 0.998, 0.002) + seq(0.01, 10, 0.01)
y5 = rnorm(1000, 0, 0.01)
plot(x5, y5, xlim = c(-2, 12), ylim = c(-2, 12), col = "blue", main = paste("correlation =", 
    cor(x5, y5)))
abline(lm(y5 ~ x5))

plot of chunk unnamed-chunk-3

Then, Why the correlation of the third plot have almost zero?

What does the correlation stand for? slope? or density? Maybe density, which is the concentration degree of dots based on linear regression line.

In linear regression, the formula is y = b0 + b1 * x (b1 is sd(y)/sd(x)*cor(x,y))

In other word, correlation is the slope of lm(z-transformed y ~ z-transformed x)

Let's see..

par(mfrow = c(2, 1))
plot(x4, y4, main = paste(" Original plot (correlation =", cor(x4, y4), ")"))
abline(lm(y4 ~ x4))
abline(0, 1, col = "pink")

plot((x4 - mean(x4))/sd(x4), (y4 - mean(y4))/sd(y4), main = paste("Z-transformed x, y plot (correlation=", 
    cor(x4, y4), ")"))
abline((y4 - mean(y4))/sd(y4) ~ (x4 - mean(x4))/sd(x4))
abline(0, 1, col = "pink")

plot of chunk unnamed-chunk-4

That's the reason for zero correlation value in horizontal line.

Tuesday, October 22, 2013

working environment within EC2 of amazon

요 몇일 테스트 할만한 amazon의 ec2 서버 환경을 만들려고 뻘짓했는데
최종적인 setting procedure 기록한다.

아래 내용은 window 환경에서 amazon의 EC2 서버에 접속해서 일함을 가정한다.

1. ec2 instance 생성
=> instance os 종류가 많은데 경험상 ubuntu server 버젼이 젤 사용하기 편하다(python package install 을 기준으로). key pair를 생성하고 이를 로컬 컴퓨터에 저장(그리고 테스트 하다 보면 instance를 여러개 만드는 경우가 있는데 delete 옵션이 없음, 대신 terminate 라는 것이 있는데 구글링 결과 terminate 하게 되면 특정 시간이 지나면 알아서 없어진다는 것 같음).

2. putty setting
일반적으로 사용하는 putty는 amazon에서 제공하는 pem 을 인식할수 없기에 puttygen을 이용해서 ppk 파일로 변환 생성해야 한다.
public DNS 사용해서 접속.
중요한건 id를 알아야 한다. 구글링하면 ec2-user니 user 니 뭐 이런 id 라고 하는데 내 경험상 key name과 동일하게 id 생성되고 이로 접속해야 한다. id 를 모르겠다면 root로 접근하면 특정 id로 접근하라고 경고 메시지가 나오는 참고.

3. pandas & django install
sudo yum update. ubuntu의 경우 기본적으로 python2.7.3 을 사용하기에 따로 2.7 버젼을 깔필요 없음. 
단순하게 "sudo apt-get install python-pandas", "sudo apt-get install python-django" 명령어 이용(혹은 sudo apt-get install python-setuptools  이 후 sudo easy_install 을 사용). django는 1.3.1 이 깔리는데 이는 "sudo easy_install --upgrade django" 로 version update 가능.

4. django port & access setting
http://thomas.broxrost.com/2008/08/21/persistent-django-on-amazon-ec2-and-ebs-the-easy-way/ 참조
ec2 management console 의 security group에서 inbound에 8000 port 추가 (apply rule change 꼭 클릭). 그뒤 sudo python manage.py runserver x-xx-xxx-51-60.us-west-2.compute.amazonaws.com:8000 로 django 서버 실행. 그 뒤 나오는 http url로 접속

Monday, September 2, 2013

Wednesday, August 28, 2013

그림으로 이해하는 닥터 배의 술술 보건의학통계

회사 차장님의 추천으로 읽게 된 통계책. 역대 내가 본 통계 책 중에 가장 정리가 쉽게 잘 되어 있는 책. 진작에 이 책 부터 봤다면 그때 그때 통계를 이해할려고 산발적으로 이루어졌던 노력들의 시간을 절약할 수 있었을 텐데라는 생각이 든다. 책의 구성 역시 너무 좋고 모든 것을 그림으로 표현하고자 하는 저자의 노력이 참으로 고맙기만 하다. 통계의 시작은 이 책으로 할 것을 강력히 추천한다.

Tuesday, August 20, 2013

interval tree

SnpEff (https://www.landesbioscience.com/journals/fly/2011FLY0072R1.pdf
:  SNPs, MNPs (multiple nucleotide polymorphism), InDels 등을 annotation 할때 쓰는 프로그램

위 논문의 내용은 말그대로 variant를 찾고 나서 그 뒤에 variant에 대한 annotation을 해주는 프로그램인 SnpEff를 사용하여 Drosophila의 strain의 SNP, MNPs, InDels 등을 annotation 한 논문.

프로그램 차원에서 보면 중요한 keyword는 
""interval forest, which is a hash of interval trees indexed by chromosome"".


Interval Trees
http://www.dgp.utoronto.ca/people/JamesStewart/378notes/22intervals/

interval 은 정수 쌍을 의미([a,b] such that a<b)
elementary interval은 interval의 end point (integer)  에 의해 생기는 partition을 의미.
예를 들어 interval x=[10,20],y=[15,25],z=[18,22] 가 있을때 아래 그림과 같이 7개의 elementary interval이 생긴다.
이 interval들을 저장하기 위한 자료 구조로 BST(binary search tree)를 이용한다. 
일단 elementary interval을 저장할 수 있는 자료 구조로 아래와 같은 tree 생성.
각 leaf node는 elementary interval을 나타나게 된다. 여기서 interval의 정보를 넣는 방법은..
단순하게 생각하면 아래와 같을 수 있다.
 문제는 too much memory !
그래서 아래와 같은 두가지 조건으로 internal node 까지 사용한다.
1.node의 범위가 interval 에 완전히 포함되고 2.그 node의 parent node 범위는 interval을 벗어나면 그 node에 interval 을 저장. 그러면 아래와 같이 interval을 저장하게 된다.


좀더 복잡한 예제를 보자면 아래와 같은 작곡가의 일생을 interval로 tree 를 만들면 
아래와 보는 바와 같다.

여기서 문제. 1910년 경에 살았던 작곡가는 누구인가라는 문제에 대한 해답은 어떻게 찾는가?
1910을 span 하고 있는 leaf에 담긴 interval이 그 해답. 곧 A,B.  그런데 만약 문제가 1880년 경이라면? 1880을 span 하고 있는 leaf는 아무 interval도 포함하고 있지 않다. 
그래서 정확하게 해당 interval을 찾는 과정은 해당 년도를 포함하고 있는 leaf에서부터 root 까지의 interval을 다 찾는 것. 다르게 이야기 하면 root 에서 해당 년도를  span 하고 있는 leaf를 찾아가면서 각 internal node의 interval을 다 찾으면 되는 것. 곧 root-to-leaf path finding문제가 된다.


Interval Tree Code for Python
http://blog.nextgenetics.net/?e=45


Return to Paper
Each interval tree is composed of nodes. Each node has five elements (i) a center point, (ii) a pointer to a node having all intervals to the left of the center, (iii) a pointer to a node having all intervals to the right of the center, (iv) all intervals overlapping the center point sorted by start position, and (v) all intervals overlapping the center point, sorted by end position