Main Content

이 페이지의 내용은 이전 릴리스에 관한 것입니다. 해당 영문 페이지는 최신 릴리스에서 제거되었습니다.

데이터를 그룹으로 분할하고 통계량 계산하기

이 예제에서는 patients.mat 데이터 파일의 데이터를 그룹으로 분할하는 방법을 보여줍니다. 그런 다음 환자 그룹에 대한 평균 체중과 BMI(체질량지수), 혈압 수치의 분산값을 계산하는 방법을 보여줍니다. 또한 테이블에 결과를 요약해 표시하는 방법도 보여줍니다.

환자 데이터 불러오기

100명의 환자로부터 수집한 샘플 데이터를 불러옵니다.

load patients

GenderSelfAssessedHealthStatus를 categorical형 배열로 변환합니다.

Gender = categorical(Gender);
SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus);
whos
  Name                            Size            Bytes  Class          Attributes

  Age                           100x1               800  double                   
  Diastolic                     100x1               800  double                   
  Gender                        100x1               330  categorical              
  Height                        100x1               800  double                   
  LastName                      100x1             11616  cell                     
  Location                      100x1             14208  cell                     
  SelfAssessedHealthStatus      100x1               560  categorical              
  Smoker                        100x1               100  logical                  
  Systolic                      100x1               800  double                   
  Weight                        100x1               800  double                   

평균 체중 계산하기

Smoker 변수를 사용하여 환자를 비흡연자와 흡연자로 나눕니다. 각 그룹의 평균 체중을 계산합니다.

[G,smoker] = findgroups(Smoker);
meanWeight = splitapply(@mean,Weight,G)
meanWeight = 2×1

  149.9091
  161.9412

findgroups 함수는 Smoker에서 생성된 그룹 번호의 벡터인 G를 반환합니다. splitapply 함수는 G를 사용하여 Weight를 두 그룹으로 분할합니다. splitapply는 각 그룹에 mean 함수를 적용하고 평균 체중을 벡터로 결합합니다.

findgroups는 그룹 식별자의 벡터를 두 번째 출력 인수로 반환합니다. Smoker에 논리값이 포함되어 있으므로 그룹 식별자는 논리값입니다. 첫 번째 그룹의 환자는 비흡연자이고 두 번째 그룹의 환자는 흡연자입니다.

smoker
smoker = 2x1 logical array

   0
   1

성별과 흡연 여부를 기준으로 환자의 체중을 분할하고, 평균 체중을 계산합니다.

G = findgroups(Gender,Smoker);
meanWeight = splitapply(@mean,Weight,G)
meanWeight = 4×1

  130.3250
  130.9231
  180.0385
  181.1429

GenderSmoker 간의 서로 다른 조합을 통해 네 가지 환자 그룹, 즉 여성 비흡연자, 여성 흡연자, 남성 비흡연자, 남성 흡연자가 식별됩니다. 네 개의 그룹과 각 그룹의 평균 체중을 테이블에 요약해 표시합니다.

[G,gender,smoker] = findgroups(Gender,Smoker);
T = table(gender,smoker,meanWeight)
T=4×3 table
    gender    smoker    meanWeight
    ______    ______    __________

    Female    false       130.32  
    Female    true        130.92  
    Male      false       180.04  
    Male      true        181.14  

T.gender는 categorical형 값을 가지며, T.smoker는 논리값을 가집니다. 이러한 테이블 변수의 데이터형은 GenderSmoker의 데이터형과 각각 일치합니다.

네 가지 환자 그룹의 BMI(체질량지수)를 계산합니다. HeightWeight를 두 개의 입력 인수로 사용하고, BMI를 계산하는 함수를 정의합니다.

meanBMIfcn = @(h,w)mean((w ./ (h.^2)) * 703);
BMI = splitapply(meanBMIfcn,Height,Weight,G)
BMI = 4×1

   21.6721
   21.6686
   26.5775
   26.4584

자기 보고를 기반으로 환자 그룹화하기

자신의 건강 상태를 PoorFair로 보고하는 환자의 비율을 계산합니다. 먼저, splitapply를 사용하여 각 그룹(여성 비흡연자, 여성 흡연자, 남성 비흡연자, 남성 흡연자)에 속한 환자의 수를 셉니다. 그런 다음 SG에 논리형 인덱싱을 사용하여, 자신의 건강 상태를 PoorFair로 보고하는 환자의 수만 셉니다. 이러한 개수로 이루어진 두 집합에서 각 그룹의 비율을 계산합니다.

[G,gender,smoker] = findgroups(Gender,Smoker);
S = SelfAssessedHealthStatus;
I = ismember(S,{'Poor','Fair'});
numPatients = splitapply(@numel,S,G);
numPF = splitapply(@numel,S(I),G(I));
numPF./numPatients
ans = 4×1

    0.2500
    0.3846
    0.3077
    0.1429

Poor 또는 Fair로 건강 상태를 보고하는 환자와 Good 또는 Excellent로 건강 상태를 보고하는 환자의 각 Diastolic 측정값에 대한 표준편차를 비교합니다.

stdDiastolicPF = splitapply(@std,Diastolic(I),G(I));
stdDiastolicGE = splitapply(@std,Diastolic(~I),G(~I));

결과를 테이블로 수집합니다. 이 환자들 중에서는 PoorFair로 건강 상태를 보고하는 여성 비흡연자가 혈압 수치에서 가장 큰 폭의 차이를 보여줍니다.

T = table(gender,smoker,numPatients,numPF,stdDiastolicPF,stdDiastolicGE,BMI)
T=4×7 table
    gender    smoker    numPatients    numPF    stdDiastolicPF    stdDiastolicGE     BMI  
    ______    ______    ___________    _____    ______________    ______________    ______

    Female    false         40          10          6.8872            3.9012        21.672
    Female    true          13           5          5.4129            5.0409        21.669
    Male      false         26           8          4.2678            4.8159        26.578
    Male      true          21           3          5.6862             5.258        26.458

참고 항목

|

관련 항목