Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Average daily temperature during year preceding study entry

    Hello, I am struggling how to caculate average daily temperature duirng one year preceding study entry.

    I have two datasets in Stata:
    1. Participants dataset
      • One row per participant
      • Variables:
        • participant_id
        • randomization_date
    2. Daily temperature dataset
      • One row per day
      • Variables:
        • temp_date (covers 2014–2016)
        • temperature (average temperature for that date)
    The trial recruited participants between 2014 and 2016.
    For each participant, I need to calculate the average daily temperature in the 365 days preceding their randomization date (i.e., from randomization_date - 365 to randomization_date - 1).

    I would be grateful for any advice on this.

    Best,

  • #2
    Hi Jadwiga, would you please provide example data generated through the -dataex- command? It's hard to work out a solution without a data sample that we can load into Stata. You might load the temperature data into a frame, generate a rolling average over the time frame, iterate through each participant, and look up the corresponding temperature for each participant. Or you might be able to compute the average for the correct window at each date in the temperature file, then merge by date. It's hard to come up with a good solution (let alone provide useful example code) without a data example.

    Comment


    • #3
      I'd process the temperature file first to create a one-date-per-line -364 days average file.

      Let's simplify it by using 7-day average (-7 day to -1 day), with rangestat it is easy to achieve:

      Code:
      * Set up fake data for demonstration
      clear
      set seed 429
      set obs 100
      gen temp_date = 19723 + _n
      gen temperature = rnormal(35, 3)
      format temp_date %td
      
      * Real code starts here:
      gsort temp_date
      * Get prior 6-day running average
      rangestat (mean) temperature, interval(temp_date -7 -1)
      * Change -7 to -365 in your own data
      * (Optional) Drop the first 6 cases because they are not based on 6 days
      replace temperature_mean = . if temp_date < date("07jan2014", "DMY")
      The temperature_mean of date Jan 7th, 2014 is now the mean of Jan 01 through Jan 06.

      With this data open, you can perform a merge 1:m with the patient data, matching by the date variables.

      Comment


      • #4
        Detail supplementing Ken Chui 's excellent answer: rangestat is from SSC.

        Comment


        • #5
          I generally prefer to use inbuilt commands unless they require writing far more code. In that spirit, you can achieve the same thing by using tssmooth ma:

          Code:
          * Set up fake data for demonstration (borrowed from #3, but extended to three years)
          clear
          set seed 429
          set obs 1096
          gen temp_date = 19723 + _n
          gen temperature = rnormal(35, 3)
          format temp_date %td
          
          * code using inbuilt commands
          tsset temp_date
          tssmooth ma double mean_temp = temperature, window(365 0 0)
          
          * code borrowed from #3, but amended to a 365 day window:
          gsort temp_date
          * Get prior 6-day running average
          rangestat (mean) temperature, interval(temp_date -365 -1)
          you can check that they produce the same result:
          Code:
          . assert mean_temp == temperature_mean

          Comment

          Working...
          X