Announcement

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

  • Counting the number of times a variable crosses a threshold

    Hello,

    My data is arranged as panel data. I have daily data on Covid-19 cases per municipality and I am trying to define a variable to count how many surges occurred in each municipality over the past year. For that I am using the growth rate of the number of infections, and I defined that an outbreak occurs every time the growth rate is higher than 0.1. Having done all that, my problem is in counting the number of times that occurs. I cannot use the code egen outbreak = sum(growth_rate_cum == 0.1), by(concelho) since the growth rate is never exactly 0.1. On the other hand, egen outbreak = sum(0.0950 <= growth_rate <= 0.1050), by(concelho) counts too many observations. Rounding the growth rate is also not an option, since most observations will round to 0.1.

    Having said that, I want to count the number of times that the growth rate goes from a value below 0.1 to a value equal or above 0.1. Is there anyway to do this?

    Thank you in advance

  • #2
    Code:
    (0.0950 <= growth_rate <= 0.1050)
    is parsed left to right like this i

    0.095 <= growth_rate ? 1 if true 0 if false

    and then

    0 or 1 <= 0.105 ? 1 if true 0 if false.

    which in turn will be true if the first comparison yielded 0 and false otherwise.

    Cutting to the chase, what you want is I think more like

    Code:
    bysort concelho (date) : gen wanted = sum(growth_rate >= 1 & growth_rate[_n-1] < 1)
    Last edited by Nick Cox; 01 Apr 2021, 13:35.

    Comment


    • #3
      Thank you very much! That was exactly what I needed

      Comment

      Working...
      X