Posts Tagged ‘code’

Create a dated log file in Matlab

Posted in Uncategorized on July 23rd, 2009 by Michael Ewens – Be the first to comment

If you change your datasets or code a lot in Matlab, it is smart to keep track of the results over time (trust me….). The ‘diary’ function allows you to record all output of your scripts to a file. Append your main .m file with the following code to create a diary/log file that is uniquely dated to the time (to the minute) that you ran the script:

date_now = clock;
date_now = strcat(num2str(date_now(1)),'_',num2str(date_now(2)),'_', num2str(date_now(3)), num2str(date_now(4)), num2str(date_now(5)));
diary(strcat('log', date_now,'.log'));

Plot multiple kernel densities on one plot in Stata

Posted in code on March 23rd, 2009 by Michael Ewens – Be the first to comment

If you want to compare kernel density estimates across years for a particular variable, putting each estimate on one graph will make it easy. The process is fairly straightforward in Stata (and even easier in Matlab…). First, we start with the simple ‘kdensity‘ command

kdensity income if year == 1990

Next, we append this command with the ‘addplot‘ function:

kdensity income if year == 1990, addplot(kdensity income if year == 1991)

and we can add even more with the ‘||’ syntax:

kdensity income if year == 1990, addplot(kdensity income if year == 1991 || kdensity income if year == 1992)

If we could use the ‘by’ option, this process would be much cleaner. Finally, we add a legend:

kdensity income if year == 1990, addplot(kdensity income if year == 1991 || kdensity income if year == 1992) legend(ring(0) pos(2) label(1 "1990") label(2 "1991") label(3 "1992"))

Strings to unique integers in Stata

Posted in code on March 22nd, 2009 by Michael Ewens – Be the first to comment

Suppose you have a string variable with a finite number of values (e.g. US states, industry, etc). The encode command plus the generate command will create a new variable that assigns unique ids to each string value:

encode your_string_variable, gen(new_string_as_int)

The labels in the edit/browse view will look like strings, so make sure you use ‘nolabel’ on any export or tab commands.

Stata to Mysql: Escaping Quotes

Posted in economics on March 16th, 2009 by Michael Ewens – Be the first to comment

If you use multiple linked databases or tables, I highly recommend linking mysql and Stata for both data merging and analysis. You can store the data in one clean place with relational tables and have all of Stata’s capabilities at hand with the odbc command. Before you start running “odbc insert” commands, you have to make sure the data is compatible with SQL queries. This means all strings need the quotes escaped. I was doing so many of these escapes that I wrote an ado file to simplify the process.

Download: escape_all_quotes

The script loops through all the variables in your dataset, checks if each is a string and if so, escapes any quotes. Although it has worked for 1000s of my “odbc insert”s, their could be bugs or speed improvements. Let me know.