Posts tonen met het label code. Alle posts tonen
Posts tonen met het label code. Alle posts tonen

donderdag 9 november 2017

Running Python on Mac

Most rookie guides are still to complex for a fairly easy install.

macOS has Python 2.7 pre-installed, you don't need Python 3.X (yet).

Additionally, install (in order):
  • Xcode (including command line tools)
  • Homebrew (a package manager)
  • Sublime Text (or TextWrangler)
Writing the syntax in Sublime Text, you need to select the build system (python), and build (=run) the code. The output is giving in the console. Code is straightforward, but graphic capabilities are poor.

Source: https://www.macworld.co.uk/how-to/mac/coding-with-python-on-mac-3635912/

maandag 6 februari 2017

Stata user command -norm-

Normalizing a variable is not so hard to do. The formula is:

normvar = (oldvar - minimum) / (maximum - minimum)

Hence you get a long way with the -summarize- command and r(min) and r(max).

However, in some cases you want to normalize to a wider range than appears in the data, e.g. rescale a variable that was coded 1-5 to 0-100, despite there not being units with either value 1 or 5. Maybe such cases exist in another pool, so you want to transform the variable in the same way. Again this is easy to do, but it may get tedious and make your syntax look ugly and confusing.

This is where the -norm- command may be of use.

Syntax

norm var [, replace generate(newvarname) scale(integer) maximum(integer) minimum(integer) [no]label]

The default scale is 1, hence range: 0-1.
By default value 0 gets the label 'Minimum' and the maximum gets a label too. You can wipe the label using the option nolabel.

Examples

norm q89, scale(100) replace
norm q93, scale(20) max(5)

Installation

Download the files and put them in your ado-folder (plus/personal) or refer to the containing folder by adopath + folderpath.

dinsdag 29 november 2016

Execute R code in Stata / Read SPSS data files

On 64-bit Windows OS and on Mac, the -usespss- command does not work. If you want to use SPSS data (or SAS), without quitting Stata, do something like this:

rsource, terminator(END_OF_R) rpath(R_pathname)
library(foreign);
rprecar<-read.spss("precar.sav", convert.f=TRUE);
rprecar
attributes(rprecar);
write.csv(rprecar,"rprecar.csv", na = ".");
q();
END_OF_R

It may be tricky to find your R_pathname. I didn't bother looking up what it is for now - probably the path to the executable (which sucks, because on every computer it will be different). In the example the conversion goes to csv, and there's the missing values ("na") option. The foreign package actually also allows old Stata 9 files, which is just fine and will preserve most labels, variable names, and missing values.

Here's the info for -foreign-:
https://cran.r-project.org/web/packages/foreign/foreign.pdf

maandag 21 november 2016

Time stamp

Making a time stamp is not very straightforward. The first two local do not deliver a nice year-month-day prefix, so we need to use a trick:

local mydate c(current_date)
local mydate=date(c(current_date),"DMY")

local mydate: di %tdCYND date(c(current_date),"DMY")
global date = `mydate'
di $date

zondag 10 april 2016

EU28 Member States in seven clusters

It's a very repetitive task to always cluster European Union Members States, but despite the heterogeneity in any possible group of clusters, it is probably the only way to have a basis for comparison.

My preferred clusters consist of the EU-15 and the New Member States, or Central and Eastern Europe. Within each group, we can further split up by broad welfare state or cultural groups. The syntax below for Stata should be self-explanatory.

gene byte eu28 = 0
replace   eu28 = 1 if inlist(geo,"BE","DE","FR","AT","NL")  // Continental Western Europe
replace   eu28 = 2 if inlist(geo,"GB","IE")     // British isles
replace   eu28 = 3 if inlist(geo,"SE","FI","NO","DK")     // Scandinavia
replace   eu28 = 4 if inlist(geo,"ES","IT","PT","GR")     // Southern Europe
replace   eu28 = 5 if inlist(geo,"HU","PL","SI","SK","CZ")  // CEE Slavic (and Hungary)
replace   eu28 = 6 if inlist(geo,"LT","LV","EE")            // CEE Baltic
replace   eu28 = 7 if inlist(geo,"HR","RO","BG","CY","MT")  // CEE Balkan and Mediterranean Isles

donderdag 22 oktober 2015

Stata code for Herfindahl concentration index

Herfindahl concentration index

The Herfindahl concentration index computes a score between 0 (total dispersion) and 1 (total concentration) which is the sum of all squared shares within a unit.

In the working example hours per job for workers are used.

tempvar t1
egen `t1' = total(hours), by(workerid year)
tempvar t2
gene `t2' = (hours / `t1')^2
egen herfindahl = total(`t2'), by(workerid year)
drop `t1' `t2'
label var herfindahl "Herfindahl concentration index"