👨‍💼

5.4.1. SAS 데이터 시각화

목차

0. 헬스장 데이터 분석

그래프를 그리기 위해 헬스장 회원의 정보를 이용해 데이터 셋을 만들어 보도록 하겠습니다.
data mysas.info; input id name $10. gender $ age height weight PT price; datalines; 1901 강지우 W 20 160 60 1 250 1902 김도윤 M 21 175 80 1 230 1903 현수아 W 20 157 55 0 120 2001 도서아 W 22 164 68 0 150 2002 김준우 M 24 169 70 0 180 1904 오현주 W 21 161 56 1 240 1905 방희지 W 20 151 53 1 260 1906 송나라 W 20 169 55 1 250 2003 도경인 M 23 189 72 1 250 2005 마지희 W 24 168 64 0 100 ; run; proc print data=mysas.info; run;
 
notion imagenotion image
 

1. 세로 막대그래프

막대그래프는 변수의 빈도수를 나타내는 그래프입니다.
title "barchart"; proc sgplot data=mysas.info; vbar age; run;
 
notion imagenotion image
 
성별의 빈도를 그래프로 나타내었을 때 20살이 많이 있는것을 볼 수 있습니다.
 

2. 가로 막대그래프

title "barchart"; proc sgplot data=mysas.info; hbar age; run;
 
notion imagenotion image
 
가로 막대 그래프는 세로막대 그래프의 코드 vbar 에서 hbar 로 바꾸어 주면 됩니다.

3. 누적 막대 그래프

title "barchart"; proc sgplot data=mysas.info; vbar age / group=gender; run;
 
group : 변수를 기준으로 그룹 간 비교를 해줍니다.
 
notion imagenotion image
 
남자와 여자를 색깔별로 나타내고 남자의 수치에 누적해서 보여줍니다.
 

4. 그룹별 막대그래프

groupdisplay : 그룹화된 표식을 클러스터로 지정을 해줍니다.
title "barchart"; proc sgplot data=mysas.info; vbar age / group=gender groupdisplay=cluster; run;
 
notion imagenotion image
 
클러스터 옵션을 주었습니다. 남여로 나누어 각각의 나이에 맞는 빈도수 그래프를 나타내는 것을 볼 수 있습니다.
 

4.1 평균 표준편차를 이용한 막대그래프 그리기

title "Barchart"; proc sgplot data=mysas.info; vbar PT / response=price limitstat=stddev limits=upper stat=mean; run;
 
upper lower 비교
 
notion imagenotion image
upper : 표준편차는 로 그려지게 됩니다.
 
notion imagenotion image
lower : 표준편차는 아래로 그려지게 됩니다.
PT를 받는사람과 받지 않는 사람의 가격의 평균, 표준편차를 이용하여 막대그래프를 그렸습니다.
화면을 분할하여 두개의 그래프를 그립니다.
title "boxplot"; proc sgpanel data=mysas.info; panelby gender; vbar PT / response=price limitstat =stddev limits=upper stat=mean; run;
 
notion imagenotion image
 
panelby에 오는 변수에 따라 panel의 개수가 정해집니다. 성별로 구분하고 PT별 price의 평균, 표준편차를 출력합니다.
 

5. 파이차트 그리기

파이 차트는 범주형 데이터를 가지고 그릴 수 있습니다. 범주형이란 숫자가 아닌 문자로 이루어진 값을 말합니다.
proc sgpie data=mysas.info; pie gender; run;
 
notion imagenotion image
 
이번에는 파이차트를 그려보았습니다. 그려본 결과 헬스장에 남자에 비해 여자가 많이 다니고 있는것을 한눈에 알아 볼 수 있습니다.
 

6. 상자그림

먼저 가격별 상자그림을 그려보도록 하겠습니다.
title "boxplot"; proc sgplot data=mysas.info; vbox weight; run;
 
notion imagenotion image
 
상자그림에서 가운데 파란색 줄은 중위수를 의미하고 마름모 모양은 평균을 의미합니다.
 
성별에 따른 가격을 상자 수염도로 나타내 보도록 하겠습니다.
title "boxplot"; proc sgplot data=mysas.info; vbox price / category=gender; run;
 
notion imagenotion image
 
여러개의 상자 그림 그리기
PT를 받는 사람을 1, 받지 않는 사람을 0으로 입력했었습니다.
이 데이터를 가지고 PT를 받는 사람과 받지 않는 사람으로 구분하고 성별에 따라 상자 수염도를 그려 보겠습니다.
Proc sgpanel 프로시저는 panelby에 오는 변수에 따라 panel의 개수가 정해집니다. Rows행의 개수, columns열의 개수 입니다.
 
title "boxplot"; proc sgpanel data=mysas.info; panelby PT / rows=1 columns=2; vbox price / category=gender; run;
 
notion imagenotion image
 

7. 히스토그램 그리기

title "histogram"; proc sgplot data=mysas.info; histogram price; run;
 
notion imagenotion image
 

7.1 변수의 정규 밀도 함수 그리기

title "histogram"; proc sgplot data=mysas.info; histogram weight; density weight; /*파랑*/ density weight / type=kernel; /*빨강*/ keylegend / location=inside position=toprightkernel; run;
 
notion imagenotion image
 
keylegend: 범례를 추가 해줍니다.
location: 그림 안/밖 범례를 지정해줍니다.
position: 그림 안/밖 범례 위치를 지정해 줍니다.
 
여러 범주에 따른 panel을 그리기
Scale을 proportion, count, percent로 지정합니다.
title "histogram"; proc sgpanel data=mysas.info; where PT=1; panelby gender age / rows=2 columns=2; histogram price / scale=proportion; run;
 
notion imagenotion image
title "histogram"; proc sgpanel data=mysas.info; where PT=1; panelby gender age / rows=2 columns=2; histogram price / scale=count; run;
 
notion imagenotion image
title "histogram"; proc sgpanel data=mysas.info; where PT=1; panelby gender age / rows=2 columns=2; histogram price / scale=percent; run;
 
notion imagenotion image
 

7.2 transparency 그래프의 불투명도를 조정하기

title "histogram"; proc sgplot data=mysas.info; histogram price; histogram height / transparency=0.5; run;title "scatter"; proc sgplot data=mysas.info; scatter x=weight y=height / group=gender; run;
 
notion imagenotion image
 
 

8. 산점도 / 매트릭스

산점도는 두 변수간의 관계를 나타낼 때 쓰이면서 그래프가 직성형태를 보이는 경우 양의 관계가 있다고 말할 수 있습니다.
title "scatter"; proc sgplot data=mysas.info; scatter x=weight y=height / group=gender; run;
 
notion imagenotion image
 

8.1 산점도에 ellipse를 함께 그리기

title "scatter"; proc sgplot data=mysas.info; scatter x=weight y=height / group=gender; ellipse x=weight y=height / type=predicted alpha=.10; run;
 
notion imagenotion image
 

8.2 선형회귀모형 만들기

선형 회귀 직선을 만들기 위해 x와 y에 변수를 지정하고 group옵션으로 PT를 하고 있는지 여부에 따른 회귀직선을 그려보겠습니다.
title "Regression Line"; proc sgplot data=mysas.info; reg x=weight y=height / group=PT; run;
 
notion imagenotion image
 

8.3 산점도에 선형회귀를 함께 그리기

title "Regression Line"; proc sgplot data=mysas.info; scatter x=weight y=height / group=PT; reg x=weight y=height / cli clm nomarkers; run;
Cli개별 예측 값에 대한 신뢰 한계를 의미하며, 그래프에 점선으로 나타납니다.
Clm평균 예측 값에 대한 신뢰 한계를 의미하며, 그래프에 파란색 면적으로 나타납니다.
 
notion imagenotion image
 

8.4산점도 매트릭스를 그리기

title "Regression Line"; proc sgscatter data=mysas.info; where PT=1; matrix weight height age price / group=gender diagonal=(histogram); run;
 
notion imagenotion image
 

9. 코로나 데이터 분석

2020년 3월 1일 부터 2020년 4월 1일의 코로나 확진자 데이터를 가지고 데이터 분석을 실행해 보도록 하겠습니다.
 
 
notion imagenotion image

9.1 지역별 기초 통계량 구하기

proc means data=covid.region; run;
 
notion imagenotion image
 
각 변수에 대하여 기초 통계량을 구해 보았습니다. 표를 보시면 각 변수에 대한 자료수, 평균, 표준편차, 최솟값, 최댓값이 나타나신 것을 볼 수 있습니다.
 

9.2 제주 확진자 데이터로 분포와 확률도표 그리기

proc univariate data=covid.region plot; var jeju; run;
 
notion imagenotion image
notion imagenotion image
 
notion imagenotion image
이렇게 기초통계량과 여러가지 그래프가 나온 것을 볼 수 있습니다.
분석 결과 3월 한달에 제주에서 하루 평균 4.75 명 정도 확진자가 발생하였습니다.
막대그래프를 보면 한달동안 하루에 4명의 확진자가 발생한 것을 볼 수 있습니다. 그리고 분위수를 이용해 상자수염도를 표현하실 수 있습니다. 관측 값을 가지고 발생 빈도를 표에 그려서 점차 증가되는 그래프를 보실 수 있습니다.
 

9.3 날짜별 대구 확진자 수 그래프

proc sgplot data=covid.region; series x=day y=daegu; run;
 
notion imagenotion image
 

9.4 코로나 감염 사망자 수 그래프

total 데이터는 코로나 검역 대상자, 확진자, 완치자, 사망자에 대한 데이터 입니다. 이 데이터에서 코로나 확진자 중 사망한 사람 수에 대한 그래프를 볼 수 있습니다.
 
proc sgplot data=covid.total; scatter x=confirmed y=deaths / markerattrs=(size=14 symbol=CircleFilled color='blue'); run;
 
notion imagenotion image
 
산점도를 그려본 결과 거의 직선형태를 띄고 있으며 코로나 확진자중에서 사망자가 많이 나오고 있으며 양의 상관관계를 가지고 두 변간 영향을 주고 있는것으로 보입니다.