Announcement

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

  • How to add a new column in stata by using a vector and how to create a column by using results from regression?

    1. I am coding in a do file, and want to add a column of year in stata , ranging from year 1989 to year 2015. May I ask the code to do it?
    2. I also want to create a column for adjusted r square from my regression model (regressed yearly from year 1988 to 2015). That is to say, 27 observations of adjusted r square. May I ask the code for this purpose?

  • #2
    It would be best to tell us first about your existing data.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      It would be best to tell us first about your existing data.
      Hi Nick. I am thrilled to your reply. I didn't come back to Statlist for a while cuase I thought my silly question wouldn't get any attention.

      Frist, I attach the table I wanna replicate for you to better understand what I am trying to do.

      Second, to answer your question. I have the data of firms' cash flow and earnings from year 1989 to year 2015. I want to extract the adjusted R square for xtreg CF_TA l.EARN_TA for each year and then list them in the database in the order of year as illusted in my attachement.

      For example, this is how I display the adjusted of R square for one year. There are two issues with my method: first, I don't know how to put the R square value in the database in the year of order; Second, I don't know how to do a loop here. If I want to extract all the adjusted R square for 27 years, I will have to repeat the following code 27 times.

      xtreg CF_TA l.EARN_TA if (fyear == 1990)
      ereturn list
      scalar rsquare = e(mss)/(e(mss)+e(rss))
      scalar r2_a = 1 - (1 - e(r2))*( e(df_r) + e(df_m) )/( e(df_r) )
      display r2_a

      Please let me know if my expression is still unclear.
      Attached Files

      Comment


      • #4
        One possible way is to turn the analysis code into a program, and then use -statsby- to apply that across years. Since you didn't use -dataex- to provide any sample data, here is one that uses the built-in auto data set as an illustration:

        Code:
        sysuse auto, clear
        
        capture program drop regProg
        program define regProg, rclass
            reg mpg weight headroom
            * Whatever you'll need to compute:
            return scalar smthng = e(r2_a)
        end
        
        * Check it out:
        regProg
        return list
        
        * Apply across different levels in rep78:
        statsby adj_r2 = r(smthng), by(rep78): regProg
        The existing data will be replaced by the requested summary statistics.

        Comment


        • #5
          Originally posted by Jiali Luo View Post

          Hi Nick. I am thrilled to your reply. I didn't come back to Statlist for a while cuase I thought my silly question wouldn't get any attention.

          Frist, I attach the table I wanna replicate for you to better understand what I am trying to do.

          Second, to answer your question. I have the data of firms' cash flow and earnings from year 1989 to year 2015. I want to extract the adjusted R square for xtreg CF_TA l.EARN_TA for each year and then list them in the database in the order of year as illusted in my attachement.

          For example, this is how I display the adjusted of R square for one year. There are two issues with my method: first, I don't know how to put the R square value in the database in the year of order; Second, I don't know how to do a loop here. If I want to extract all the adjusted R square for 27 years, I will have to repeat the following code 27 times.

          xtreg CF_TA l.EARN_TA if (fyear == 1990)
          ereturn list
          scalar rsquare = e(mss)/(e(mss)+e(rss))
          scalar r2_a = 1 - (1 - e(r2))*( e(df_r) + e(df_m) )/( e(df_r) )
          display r2_a

          Please let me know if my expression is still unclear.
          Hi Ken, Thank you for the code. It is very helpful. I am sorry that I didn't know I could use -dataex- and didn't provide the data. I will definitely try -dataext- next time

          Comment

          Working...
          X