Announcement

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

  • How to keep only ids that install a solar panel (dummy: 0/1) at a given point in time, and then create a transition matrix?

    Hi everyone,

    I have a panel data of households, with the start and the end of their electricity contracts.

    I would like to keep only observations for ids that install solar panels at a point in time (so that switch from 0 to 1 at any moment for my solar_dummy variable).

    Imagine initially that a household doesn't own a solar panel on, say, December 1, 2021, and decides to install it later, on, say, April 5, 2023. I'd like to keep these households, please.
    On the other hand, I don't want to keep those who have had a solar panel since the beginning, or who have never installed one, please.

    Then, I would like to construct a transition matrix and observe if their electricity tariffs change (tariff_ekon_id_encod).
    • Could anyone give me a hand with that, please?
    Here is a small dataex:


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id double date_contract_start long tariff_ekon_id_encod float solar_dummy
    1001 18887 1 0
    1001 21701 2 0
    1001 22432 4 0
    1001 22646 4 0
    1001 22677 4 0
    1001 22736 4 0
    1001 23011 4 0
    1001 23070 4 0
    1005 18800 1 0
    1005 21640 1 0
    end
    format %td date_contract_start
    label values tariff_ekon_id_encod tariff_ekon_id_encod
    label def tariff_ekon_id_encod 1 "20A", modify
    label def tariff_ekon_id_encod 2 "20DHA", modify
    label def tariff_ekon_id_encod 4 "20TD", modify
    Thank you very much in advance.
    Michael

  • #2
    Hi Michael,
    regarding the first part of your question, I believe the following code is what you are after:

    Code:
    bysort id (date_contract_start): gen changers = solar_dummy[_N]!=solar_dummy[1] & solar_dummy[1]==0
    For the second part of your question you should provide more information to seek help. Stata has two commands that may turn out useful in this setting (tsfill and xttrans)

    Comment


    • #3
      Step #1 can be done as follows:

      Code:
      bysort id : egen byte got_solar = max(solar_dummy)
      The idea is that for a binary coded variable (0 or 1), the max() and min() operations can be interpreted as ever or never taking on the characteristic implied by a value of 1. Missings are implicitly ignored, if they exist.

      As for transition matrices, I can't help you, and I think you've asked for related help already on that topic.

      Comment


      • #4
        Hi,

        Luca Calianno: Thanks for the shared code. It is indeed what I am looking for!

        Lovely morning/day/evening. Thanks for your time.
        Michael
        Last edited by Michael Duarte Goncalves; 13 Dec 2023, 01:13.

        Comment


        • #5
          Hi Leonardo Guizzetti,

          I would like to thank you for the shared code. It will help me a lot.
          Indeed, I already asked about transition matrices. Unfortunately, I still haven't received a reply... Probably my question is badly explained, or too complicated.

          I wish you all the best. Thanks again for your help (and time!)
          Best,

          Michael

          Comment


          • #6
            Originally posted by Luca Calianno View Post
            Hi Michael,
            regarding the first part of your question, I believe the following code is what you are after:

            Code:
            bysort id (date_contract_start): gen changers = solar_dummy[_N]!=solar_dummy[1] & solar_dummy[1]==0
            For the second part of your question you should provide more information to seek help. Stata has two commands that may turn out useful in this setting (tsfill and xttrans)
            Hi Everyone,

            I tried -xtset-, but does not work with me. Here is the error given:

            [CODE]

            xtset id date_contract_start
            repeated time values within panel
            r(451);

            [CODE]

            So I want to know if there are other means to do that, please. Basically I would like to have a matrix with the "Previous Tariff" in the Columns and "Current Tariff" in rows. Columns should sum up to 1.
            Any help you might provide is appreciated!

            Thank you so much in advance!
            Michael

            Comment


            • #7
              1. The issue you are reporting is given by the fact that there are two or more identical obsertvation in your dataset.
              Naively you can run (but you should check previously what is the reason why this is happening)
              duplicates drop id date_contract_start

              2. I still have some questions about this transition matrix, but, I would say that it would help a lot if you could provide a small example of the matrix you are after, even only with two households.

              Comment


              • #8
                Hi Luca Calianno,

                I am sharing with you the kind of matrix that I would like to have, in .png format. But I would like that only for solar panel owners, please.
                Thanks again for your interest and most of all your help!
                Attached Files

                Comment


                • #9
                  I am really sorry if i am not getting your point.
                  Why does this not work?
                  Code:
                  /* edit:
                  add:
                  duplicates drop id date_contract_start, force
                  or:
                  collapse tariff_ekon_id_encod, by(id date_contract_start)
                  */
                  xtset id date_contract_start
                  
                  xttrans tariff_ekon_id_encod

                  Comment


                  • #10
                    This is exactly what I want!

                    Thank you so much. I apologize for the confusion.

                    Have a nice day Luca Calianno!

                    Michael

                    Comment

                    Working...
                    X