Announcement

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

  • Plotting local variables

    Hi all,

    so my problem is the following. I am creating this difference: valore_aggiunto_`k'[1]- valore_aggiunto_`k'[15] within a for loop. This difference consists in a number. Since i = 0.02(0.05)5.5, I have 110 of these numbers.
    the code generating them looks as follows:

    Code:
    local k=0
    forvalues i = 0.02(0.05)5.5{
        quietly gen v_a_`k'= median_log_sales_norm if normaliz==1
        quietly replace v_a = median_log_sales_norm/(1+`i')^1 if normal==2
        quietly replace v_a = median_log_sales_norm/(1+`i')^2 if normal==3
        quietly replace v_a = median_log_sales_norm/(1+`i')^3 if normal==4
        quietly replace v_a = median_log_sales_norm/(1+`i')^4 if normal==5
        quietly egen valore_aggiunto_`k' = sum(v_a), by(Tech)
        di "The diff. in valori aggiunti with tasso `i' is:"valore_aggiunto_`k'[1]- valore_aggiunto_`k'[15]
        quietly drop v_a_`k' valore_aggiunto_`k'
        quietly local k=`k'+1
    }
    and the dataset is:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 Tech float(normalized_year median_log_sales_norm)
    "Cell"           1 .063966386
    "Cell"           2   .0805286
    "Cell"           3   .1054098
    "Cell"           4  .12791473
    "Cell"           5   .1911684
    "Gene"           1 .014278216
    "Gene"           2  .02447586
    "Gene"           3  .03772044
    "Gene"           4   .0517167
    "Gene"           5  .07924227
    "Monoc_antibody" 1 .008099749
    "Monoc_antibody" 2 .013124235
    "Monoc_antibody" 3 .016857458
    "Monoc_antibody" 4  .02265201
    "Monoc_antibody" 5 .031008275
    end
    having 15 observations.
    Now, what I would like to do is to make a plot with y-axis the 110 values valore_aggiunto_`k'[1]- valore_aggiunto_`k'[15], and as x-axis the 110 values of the i (an interest rate).
    I thought about defining a global variable or a tempfile, but did not manage to do that.

    Can you please help me?

    Thank you,

    Federico




  • #2
    If I understand this correctly, you have a loop over 110 distinct values and a result for each.

    The use of local macros (best not called local variables: see https://www.stata.com/statalist/arch.../msg01258.html) in defining variable names is a distraction here. You just need to make your dataset larger and save a result each time around the loop.

    I am queasy about looping over non-integers -- please see https://journals.sagepub.com/doi/pdf...867X1001000115 -- hence the rewriting of that part of your code. Note also that your 5 distinct cases collapse to 1.

    You'll supply better axis titles for your graph.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 Tech float(normalized_year median_log_sales_norm)
    "Cell"           1 .063966386
    "Cell"           2   .0805286
    "Cell"           3   .1054098
    "Cell"           4  .12791473
    "Cell"           5   .1911684
    "Gene"           1 .014278216
    "Gene"           2  .02447586
    "Gene"           3  .03772044
    "Gene"           4   .0517167
    "Gene"           5  .07924227
    "Monoc_antibody" 1 .008099749
    "Monoc_antibody" 2 .013124235
    "Monoc_antibody" 3 .016857458
    "Monoc_antibody" 4  .02265201
    "Monoc_antibody" 5 .031008275
    end
    
    set obs 110 
    
    numlist "2(5)550"
    tokenize "`r(numlist)'"
    gen y = . 
    gen x = . 
    
    quietly forvalues i = 1/110 {
        local I = ``i''/100 
        gen v_a = median_log_sales_norm / (1 + ``i''/100)^(normal - 1)
        egen valore_aggiunto = sum(v_a), by(Tech)
        
        noisily di "The diff. in valori aggiunti with tasso `I' is: "valore_aggiunto[1]- valore_aggiunto[15]
    
        replace y = valore_aggiunto[1]- valore_aggiunto[15] in `i'
        replace x = `I' in `i'
        drop v_a  valore_aggiunto 
    }
    
    scatter y x

    Comment


    • #3
      Perfect. Thank you very much!

      Comment

      Working...
      X