Announcement

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

  • Exponential function not working

    I had replaced my dataset with log(x) function once I was told not to work with such large numbers in my regression. However, now I need some of my original values that have not been saved--huge mistake!

    Basically I started off like this:
    replace population = log(population)
    rename population logpopulation

    And now I've tried "generate population2 = exp(logpopulation)"
    However, the numbers do not look right. Is there another formula to reverse the log function?




  • #2
    No, the exp() function is the only way to invert the log() function.

    But what do you mean when you say the numbers "do not look right?"


    With the horse apparently out of the barn, it's a bit late to lock the barn door, but a couple of general principles of data management are worth noting for future reference:

    1. Never overwrite your original data! In fact, the first thing I usually do when I get a new data set is make it read-only. The next is to make a copy and keep that in a safe place. Then, and only then, do I import it to Stata (if it didn't come as a Stata data set) and clean the data to make a first working data set, which I save as a Stata file.

    2. When you transform variables for purposes of analysis, it is usually a good idea to add the transformed variables to your data set, not replace existing variables. If this makes the data set too unwieldy to work with, then delete the originals if need be, but don't save this new data set under the same name as the first cleaned working data set. If the transformations are fast, there is no need to save it at all (because of #3 below). If the transformations are time-consuming and you will need to do additional analyses later, then save this as a different file with a new name.

    3. Also, all of the code used to transform the variables should be executed from a do-file and the results saved in a log-file so you can replicate your work later.

    Comment


    • #3
      The formulas are fine, see below for an example. What you did was pretty dangerous, over-writing your original values, so we'll never know what happened.

      If at all possible, use generate population_log=log(population), and work with the new variable. That way, you always have the originals around just in case. Can you still do this, or is the original value for population irrevocably gone?

      Code:
      clear
      set obs 1000
      *=====generate 1000 cases with varying ranges
      forvalues i=1/10 {
      quietly: gen x_`i'=runiform()*(`i'^10)
      }
      sum
      
      *=====log them
      forvalues i=1/10 {
      quietly: gen log_x_`i'=log(x_`i')
      }
      sum
      
      *======transform back
      forvalues i=1/10 {
      quietly: gen back_x_`i'=exp(log_x_`i')
      }
      sum
      Last edited by ben earnhart; 13 Feb 2015, 10:20.

      Comment

      Working...
      X