Announcement

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

  • Stata (Struggling to Obtain Covariance Matrix from Multi-Company Data)

    Hello, I have been trying to obtain a covariance matrix for quite some time. The problem gets a bit more complicated, as I have the data arranged in such a way that I have 174 rows (every 29 rows represent a different company, with a total of 6 companies). I also have a column with the returns (attached image). I haven't found a direct way to obtain a covariance matrix, so I thought of transforming this dataset into 29 rows (one for each year) and 6 additional columns (for the 6 companies) with their respective returns. Then, that way, I could run a correlate v1 v2 v3 v4 v5 v6, covariance. However, I am stuck on organizing the data, as I can't make that modification either.I would appreciate it if someone could help me.
    Click image for larger version

Name:	Captura de pantalla (95).png
Views:	1
Size:	101.5 KB
ID:	1766805
    Click image for larger version

Name:	Captura de pantalla (94).png
Views:	1
Size:	99.2 KB
ID:	1766806

  • #2
    Code:
    reshape wide RET COMNAM, i(DATEV) j(CUSIP)
    will give you one observation per year and one return variable per company.

    You do not provide example data in a usable form--there is no way to import a screen shot into Stata. So this code is not tested and may be incorrect. If so, when posting back please use the -dataex- command to show your example data. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    More generally, please read the forum FAQ for excellent advice about the most effective ways to post questions and maximize your chance of getting a timely and helpful response.
    Last edited by Clyde Schechter; 01 Nov 2024, 18:42.

    Comment


    • #3
      Clyde Schechter I want to thank you for your help and for the advice on using the dataex command. The code you suggested worked perfectly, so I really appreciate it! I will also keep your recommendations in mind for my future posts. I wasn’t aware of these things since this is my first time in this forum.

      Thanks again for your support.

      Comment


      • #4
        jorge pino

        It's excellent that you got a good answer, and quickly.

        However, when you started a new thread you got a standard message



        Increase your chances of a reply: Organize your thoughts. Write them clearly. Preview what you write before posting.

        Asking a question? Search first using the Search box at the top right. Separately search the pre-April 2014 archives.

        If you want to include a simple data example in your post, please consider using dataex (type ssc install dataex in Stata's Command window to install).

        First read Advice on Posting.
        so "first time in this forum" -- understood, but everyone gets this advice when starting a new thread.
        Last edited by Nick Cox; 02 Nov 2024, 05:42.

        Comment

        Working...
        X