728x90

[SAS]

DATA a1;                              
INPUT gender $ wei hei age;           
CARDS;                                
F 65 171 23                            
F 66 172 24                            
F 69 176 38                            
M 67 173 43                           
M 68 177 40                           
M 72 178 42                           

PROC CORR;VAR wei hei age;
RUN; 

[R]

gender <- c("F","F","F","M","M","M")
wei <- c(60,61,63,67,68,69)
hei <- c(171,173,176,174,175,178)
age <- c(23,24,38,43,40,42)
cor(hei,wei)                    #  0.958017
cov(hei,wei)                    #  3.8
cor(hei,wei,method="pearson")   #  0.958017
cor(hei,wei,age)                # 에러
cor(cbind(hei,wei,age))

 

[Python]

1. 넘파이로 상관계수 구하기

import numpy as np
gender=["F","F","F","M","M","M"]
wei = [65,66,69,67,68,72]
hei = [171,172,176,173,177,178]
age=[23,24,38,43,40,42]
np.corrcoef(wei,hei)
np.cov(wei,hei)

result = np.corrcoef(wei,hei)
print(result)
type(result)  # numpy.ndarray

2. 팬다스로 상관계수 구하기

import pandas as pd
gender=["F","F","F","M","M","M"]
wei = [65,66,69,67,68,72]
hei = [171,172,176,173,177,178]
age=[23,24,38,43,40,42]
health = pd.DataFrame({'gender':gender,'wei':wei,'hei':hei,'age':age})
health
health.corr()
health.cov()

+ Recent posts