Skip to text
wachter home >teaching >Demography 210 > computing tips

Ken Wachter - Demography 210 Computing Tips

Fall Semester 2003, U.C. Berkeley

*Invoking and Quitting from R or SPLUS

To complete the first starred exercise for Week I you will need to have access to the R statistical system (or to its predecessor Splus). If you already have an account on another computer network with access to Splus or R, you do not need an account on the Demography Network, and you are free to work with the system familiar to you. If you have no account with access to R, please consult with Prof. Wachter about obtaining a Demography account and a brief guide to using the system. The Demography ``Lab Owner's Manual'' will be found on the web at www.demog.berkeley.edu/DemogLab.

Following instructions in the ``Owner's Manual'', log yourself in and bring up a command window. Launch the R statistical system by typing

R

To quit from R when you are finished, type

q()

The letter "q" stands for quit, and it must be followed by an open parenthesis and then by a closed parenthesis, because R treats each such command as a function, and expects at least a dummy (or blank) argument between parentheses at the conclusion of a function. Then exit from your window by typing

exit

and log out of your machine by typing

logout

*Simple calculations with R or Splus

The first starred exercise for Week I helps you get acquainted with the R System or the Splus System by doing a calculation involving logarithms of U.S. population counts from early censuses. This sheet gives some examples of commands. If you type them in order, they will produce the solution to a shorter version of your problem with three censuses instead of ten. After you have learned how to use these commands, change them so as to include all ten censuses.

Any line beginning with # is a comment. It is for your information. You need not type it. Each time you type a command, hit the return key. The system will then prompt you with > and you can type another command. Note that R and Splus distinguish between capital letters and small ones, so be careful. For instance, X and x would stand for two different variables. Note also that the symbol _ (which tells R or Splus to set something equal to something else) is typed with the shift key and the underline key together.

# Anything beyond # on a line is a comment.

# We set W to be a vector with components 1790, 1800, and 1810.

W _ c( 1790, 1800, 1810 )

# We subtract 1800 from each component of W to obtain the vector X:

X _ W - 1800

# We set Z to be a vector with values 4, 5, and 7.

# These are the U.S. populations in 1790,1800, and 1810.

# They are rounded to the nearest million.

Z _ c( 4, 5, 7 )

# We set Y to be a vector whose components

# are the logarithms of 4,5, and 7.

Y _ log(Z)

# To see the components of X, just type X :

X

# To find the mean of the components of X, type mean(X):

mean(X)

# Vectors multiply by multiplying their components.

X*Z

# To see a plot of X versus Z, type

plot(X,Z)

# Calculate the numerator and denominator that you need:

numer _ mean(X*Y)- mean(X)*mean(Y)

denom _ mean(X*X)- mean(X)*mean(X)

slope _ numer/denom

# Now ask R or Splus to print the value of slope on the screen:

print(slope)

# This is your answer. Now you can do your own version.

# When you have finished, be sure to quit by typing

q( )


return to: Kenneth Wachter teaching Kenneth Wachter home page Demography home page