Details
.
Zelig uses the syntax of R, which has several essential elements:
- R is case sensitive. Zelig, the package or library, is
not the same as zelig, the command.
- R functions accept user-defined arguments: while some arguments
are required, other optional arguments modify the function's default
behavior. Enclose arguments in parentheses and separate multiple
arguments with commas. For example, print(x) or print(x,
digits = 2) prints the contents of the object x using the
default number of digits or rounds to two digits to the right of the decimal
point, respectively. You may nest commands as long as each has its
own set of parentheses: log(sqrt(5)) takes the square root
of 5 and then takes the natural log.
- The <- operator takes the output of the function on the
right and saves them in the named object on the left. For example,
z.out <- zelig(...) stores the output from zelig() as
the object z.out in your working memory. You may use z.out as an argument in other functions, view the output by
typing z.out at the R prompt, or save z.out to a file
using the procedures described in Section .
- You may name your objects anything, within a few constraints:
- You may only use letters (in upper or lower case) and
periods to punctuate your variable names.
- You may not use any special characters (aside from the
period) or spaces to punctuate your variable names.
- Names cannot begin with numbers. For example, R will not
let you save an object as 1997.election but will let
you save election.1997.
- Use the names() command to see the contents of R
objects, and the $ operator to extract elements from R
objects. For example:
# Run least squares regression and save the output in working memory:
> z.out <- zelig(y ~ x1 + x2, model = "ls", data = mydata)
# See what's in the R object:
> names(z.out)
[1] "coefficients" "residuals" "effects" "rank"
# Extract and display the coefficients in z.out:
> z.out$coefficients
- All objects have a class designation which tells R how to
treat it in subsequent commands. An object's class is generated
by the function or mathematical operation that created it.
- To see a list of all objects in your current workspace, type:
ls(). You can remove an object permanently from memory
by typing remove(goo) (which deletes the object goo), or remove all the objects with remove(list = ls()).
- To run commands in a batch, use a text editor (such as the
Windows R script editor or emacs) to compose your R commands, and
save the file with a .R file extension in your working
directory. To run the file, type source("Code.R") at the R
prompt.
If you encounter a syntax error, check your spelling, case,
parentheses, and commas. These are the most common syntax errors, and
are easy to detect and correct with a little practice. If you
encounter a syntax error in batch mode, R will tell you the line on
which the syntax error occurred.
Gary King
2011-11-29