Main Content

さまざまなメソッドを使用した timetable 変数の時間再調整と同期

この例では、さまざまな変数に対応するさまざまなメソッドを使用して、timetable 変数のギャップを埋める方法について説明します。timetable の VariableContinuity プロパティを使用して、各 timetable 変数に連続または離散データを含めるかどうかを指定することができます。関数 retime を使用して timetable をリサンプリングする場合、retimeVariableContinuity プロパティの値に応じて、以前の値を使用して内挿または入力するか、あるいは欠損データ インジケーターを使用して入力します。同様に、関数 synchronize は、入力 timetable の VariableContinuity プロパティに基づいて値を内挿または入力します。

timetable の作成

2017 年 5 月の数日間の気象シミュレーション測定値をもつ timetable を作成します。timetable 変数 TmaxTmin には各日の最高温度測定値と最低温度測定値が含まれます。PrecipTotal にはその日の合計降水量が含まれます。WXEvent は categorical 配列であり、雷やあられといった特定の種類の気象事象が特定の日に発生したかどうかが記録されます。timetable には 2017 年 5 月 4 日から 5 月 10 日までのシミュレーション データが含まれていますが、5 月 6 日と 5 月 7 日の 2 日間のデータが欠損しています。

Date = [datetime(2017,5,4) datetime(2017,5,5) datetime(2017,5,8:10)]';
Tmax = [60 62 56 59 60]';
Tmin = [44 45 40 42 45]';
PrecipTotal = [0.2 0 0 0.15 0]';
WXEvent = [2 0 0 1 0]';
WXEvent = categorical(WXEvent,[0 1 2 3 4],{'None','Thunder','Hail','Fog','Tornado'});
Station1 = timetable(Date,Tmax,Tmin,PrecipTotal,WXEvent)
Station1=5×4 timetable
       Date        Tmax    Tmin    PrecipTotal    WXEvent
    ___________    ____    ____    ___________    _______

    04-May-2017     60      44         0.2        Hail   
    05-May-2017     62      45           0        None   
    08-May-2017     56      40           0        None   
    09-May-2017     59      42        0.15        Thunder
    10-May-2017     60      45           0        None   

連続および離散 timetable 変数のリサンプリング

欠損している 2 日間のデータを埋める 1 つの方法は、関数 retime を使用することです。メソッドを指定せずに retime を呼び出すと、retime が欠損データ インジケーターを使用してギャップを埋めます。たとえば、retime は数値変数のギャップを NaN 値で、categorical 変数のギャップを未定義の要素で埋めます。

Station1Daily = retime(Station1,'daily')
Station1Daily=7×4 timetable
       Date        Tmax    Tmin    PrecipTotal      WXEvent  
    ___________    ____    ____    ___________    ___________

    04-May-2017     60      44         0.2        Hail       
    05-May-2017     62      45           0        None       
    06-May-2017    NaN     NaN         NaN        <undefined>
    07-May-2017    NaN     NaN         NaN        <undefined>
    08-May-2017     56      40           0        None       
    09-May-2017     59      42        0.15        Thunder    
    10-May-2017     60      45           0        None       

retime を呼び出すときにメソッドを指定すると、同じメソッドを使用してあらゆる変数のギャップを埋めます。異なる変数に異なるメソッドを適用するには、retime を複数回呼び出し、各呼び出しにおいて timetable にインデックスを付けて異なる変数のサブセットにアクセスすることができます。

ただし、timetable の VariableContinuity プロパティを指定することで、異なるメソッドを適用することもできます。各変数が連続または離散データを含むかどうかを指定できます。これにより、関数 retime は、対応する VariableContinuity の値に応じて、各 timetable 変数に異なるメソッドを適用します。

VariableContinuity を指定する場合、関数 retime は以下のメソッドを使用して出力 timetable 変数に入力します。

  • 'unset' — 該当する型 (数値変数用の NaN など) の欠損データ インジケーターを使用して値を入力します。

  • 'continuous' — 線形内挿を使用して値を入力。

  • 'step' — 以前の値を使用して値を入力。

  • 'event' — 該当する型の欠損データ インジケーターを使用して値を入力します。

Station1 の気温データは連続、PrecipTotal はステップ データ、および WXEvent はイベント データであることを指定します。

Station1.Properties.VariableContinuity = {'continuous','continuous','step','event'};
Station1.Properties
ans = 
  TimetableProperties with properties:

             Description: ''
                UserData: []
          DimensionNames: {'Date'  'Variables'}
           VariableNames: {'Tmax'  'Tmin'  'PrecipTotal'  'WXEvent'}
    VariableDescriptions: {}
           VariableUnits: {}
      VariableContinuity: [continuous    continuous    step    event]
                RowTimes: [5x1 datetime]
               StartTime: 04-May-2017
              SampleRate: NaN
                TimeStep: NaN
                  Events: []
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

Station1 内のデータをリサンプリングします。VariableContinuity に代入されている値により、関数 retime によって気温データが内挿され、PrecipTotal に前日のデータの値が入力され、未定義の要素を使用して WXEvent が入力されます。

Station1Daily = retime(Station1,'daily')
Station1Daily=7×4 timetable
       Date        Tmax     Tmin     PrecipTotal      WXEvent  
    ___________    ____    ______    ___________    ___________

    04-May-2017     60         44        0.2        Hail       
    05-May-2017     62         45          0        None       
    06-May-2017     60     43.333          0        <undefined>
    07-May-2017     58     41.667          0        <undefined>
    08-May-2017     56         40          0        None       
    09-May-2017     59         42       0.15        Thunder    
    10-May-2017     60         45          0        None       

メソッドを指定した場合、VariableContinuity の値をオーバーライドすることで、retime はそのメソッドをすべての変数に適用します。

Station1Missing = retime(Station1,'daily','fillwithmissing')
Station1Missing=7×4 timetable
       Date        Tmax    Tmin    PrecipTotal      WXEvent  
    ___________    ____    ____    ___________    ___________

    04-May-2017     60      44         0.2        Hail       
    05-May-2017     62      45           0        None       
    06-May-2017    NaN     NaN         NaN        <undefined>
    07-May-2017    NaN     NaN         NaN        <undefined>
    08-May-2017     56      40           0        None       
    09-May-2017     59      42        0.15        Thunder    
    10-May-2017     60      45           0        None       

連続および離散データを含む timetable の同期

関数 synchronize も、各入力 timetable の VariableContinuity プロパティで指定された値に応じて、異なるメソッドを使用して出力 timetable 変数を入力します。

2 番目の気象計からの圧力測定値 (ミリバール単位) を含む 2 番目の timetable を作成します。timetable には 2017 年 5 月 4 日から 5 月 8 日までのシミュレーション測定値が含まれています。

Date = datetime(2017,5,4:8)';
Pressure = [995 1003 1013 1018 1006]';
Station2 = timetable(Date,Pressure)
Station2=5×1 timetable
       Date        Pressure
    ___________    ________

    04-May-2017       995  
    05-May-2017      1003  
    06-May-2017      1013  
    07-May-2017      1018  
    08-May-2017      1006  

関数 synchronize を使用して 2 つの気象計からのデータを同期します。synchronize は、Station1VariableContinuity プロパティの値に従って Station1 の変数の値を入力します。ただし、Station2VariableContinuity プロパティは空であるため、synchronizeNaN の値を使用して Pressure を入力します。

BothStations = synchronize(Station1,Station2)
BothStations=7×5 timetable
       Date        Tmax     Tmin     PrecipTotal      WXEvent      Pressure
    ___________    ____    ______    ___________    ___________    ________

    04-May-2017     60         44        0.2        Hail              995  
    05-May-2017     62         45          0        None             1003  
    06-May-2017     60     43.333          0        <undefined>      1013  
    07-May-2017     58     41.667          0        <undefined>      1018  
    08-May-2017     56         40          0        None             1006  
    09-May-2017     59         42       0.15        Thunder           NaN  
    10-May-2017     60         45          0        None              NaN  

Station2.Pressure に連続データが含まれていることを示すには、Station2VariableContinuity プロパティを指定します。Station2 に含まれている変数は 1 つのみですが、文字ベクトルではなく cell 配列を使用して VariableContinuity を指定しなければなりません。

Station2.Properties.VariableContinuity = {'continuous'};
Station2.Properties
ans = 
  TimetableProperties with properties:

             Description: ''
                UserData: []
          DimensionNames: {'Date'  'Variables'}
           VariableNames: {'Pressure'}
    VariableDescriptions: {}
           VariableUnits: {}
      VariableContinuity: continuous
                RowTimes: [5x1 datetime]
               StartTime: 04-May-2017
              SampleRate: NaN
                TimeStep: 1d
                  Events: []
        CustomProperties: No custom properties are set.
      Use addprop and rmprop to modify CustomProperties.

2 つの気象計からのデータを同期します。Station2.Pressure には連続データが含まれているため、synchronize によって BothStations.Pressure の値が入力されます。

BothStations = synchronize(Station1,Station2)
BothStations=7×5 timetable
       Date        Tmax     Tmin     PrecipTotal      WXEvent      Pressure
    ___________    ____    ______    ___________    ___________    ________

    04-May-2017     60         44        0.2        Hail              995  
    05-May-2017     62         45          0        None             1003  
    06-May-2017     60     43.333          0        <undefined>      1013  
    07-May-2017     58     41.667          0        <undefined>      1018  
    08-May-2017     56         40          0        None             1006  
    09-May-2017     59         42       0.15        Thunder           994  
    10-May-2017     60         45          0        None              982  

synchronize への入力引数としてメソッドを指定する場合、関数 retime の場合と同様に、synchronize によってそのメソッドがすべての変数に適用されます。

参考

| |

関連するトピック