POLI 279: C by the Beach




August 17 to September 18
(Optional) Fall Quarter, AY 2009-10
Department of Political Science
University of California, San Diego
La Jolla, CA 92093-0521


Instructor: Keith T. Poole

Office: SSB 368
E-Mail: kpoole@ucsd.edu
WebSite: Voteview Home Page


Recommended Reference Texts:


C

MINGW -- Minimalist GNU for Windows
How to Install MINGW for Windows Machines
In addition to the instructions below, in order to install the GCC compiler on Macs, there is an additional step that you will need to do to get Fink Commander to install GCC properly. You will need to download the Xcode developer tools as well. To do this, do the following.

How to Install GCC for MAC OS 10.5

C Standard Library Description in Wikipedia

LAPACK (Linear Algebra PACKage) Description in Wikipedia
LAPACK Netlib Home Page
Listing of the Key LAPACK Subroutines
Computes eigenvalues and eigenvectors of a real symmetric matrix. Very useful for Many Empirical Applications.
Matrix Inversion Routine.
Computes the singular value decomposition (SVD) of a general rectangular matrix. Used in a host of important empirical applications.
Lapack.h file used by R
BLAS (Basic Linear Algebra Subprograms) Description in Wikipedia
BLAS Netlib Home Page
dgemm.f -- Used in OLS.C -- Computes the matrix operation: alpha*(A_transpose*B) + beta*C, where A, B, and C are matrices and alpha and beta are scalers. Note that if A=B, alpha=1, and beta=0, then this routine returns A_transpose*A.
dgesv.f -- LAPACK Routine Used in OLS.C -- Solves the linear equation problem of A * X = B. Note that if B is set equal to the identity matrix, then the routine returns A_inverse.
C Programs From Jones and Aitken

R

The Comprehensive R Archive Network (CRAN) (Download R from this Site)

Rtools for Development of Stat Packages for R (Download Rtools from this Site)

An Introduction to R. (R-intro.pdf -- Reference Work by R Development Core Team)

Using R for Data Analysis and Graphics: An Introduction. (usingR.pdf -- Reference Work by J. H. Maindonald on R Graphics)

Outline of Topics
  1. Origins and Basic Structure of the C Programming Language

    Suggested Readings

    Introductory Programs (Courtesy of James Lo and Keith Poole):

    Hello.c
    fahrenheit.c
    fahrenheit_real.c
    Vector.c
    random_test.c
    uniform_histogram.r (R program to plot pseudo-random uniform numbers from C program)
    function_example_10000.txt (data read by R program)
    Array.c
    Pointer_simple.c
    Pointer_declaring.c
    Control.c

    Day_One_Aug_17_2009.mp3 (MP3 file for first two hours -- 163meg)
    Day_Two_Aug_19_2009.mp3 (MP3 file for second two hours -- 160meg)
  2. Reading Data Matrices in C With Comparisons to R

    Suggested Readings

    Introductory Programs (Courtesy of James Lo and Keith Poole):

    Pointer_temperature.c
    Function_example.c
    writedata.c -- Produces Data.txt which is read in by readdata.c and Data2.txt which is read in by readdata_matrix.c
    readdata.c -- Reads Data.txt
    readdata_eof.c -- Reads Data.txt and illustrates how to use the end of file command and break
    readdata_matrix.c -- Reads Data2.txt
    ols.c -- Illustrates the use of the LAPACK and BLAS matrix/vector libraries.
    writedata_ols.c Writes Out Matrix for ols_read.c
    ols_read.c General OLS program
    ols_read_svd.c General OLS program -- does a Singular Value Decomposition on the Input X matrix to check for colinearity
    binary.c binary.c (Courtesy of Chris Fariss)
    Documentation of Binary.c
    writedata_ols_erf.c Writes Out Matrix with Normal Error for for ols_read_svd.c. Also writes data_normal.txt.
    normal_histogram.r (R program to plot pseudo-random normal distribution numbers from C program)
    data_normal.txt (data read by R program)
    ols_read_svd_general.c General OLS program that computes standard Regression Statistics -- it also does a Singular Value Decomposition on the Input X matrix to check for colinearity (updated, 30 August 2009)
    ols_read_svd_general_bush.c General OLS program set up for the Bush 2000 Election data
    ols_read_svd_general_gore.c General OLS program set up for the Gore 2000 Election data
    bush2000.txt Bush 2000 election data -- 432 Congressional Districts with 8 variables
    gore2000.txt Gore 2000 election data -- 432 Congressional Districts with 8 variables
    106_House_Example.log STATA log file for Bush and Gore Regressions


    Day_Three_Aug_21_2009.mp3 (MP3 file for third two hours -- 164meg)
    Day_Four_Aug_24_2009.mp3 (MP3 file for fourth two hours -- 171meg)
    Day_Five_Aug_26_2009.mp3 (MP3 file for fifth two hours -- 174meg)
    Day_Six_Aug_28_2009_001.mp3 (MP3 file for sixth two hours (Part 1) -- 16meg)
    Day_Six_Aug_28_2009_002.mp3 (MP3 file for sixth two hours (Part 2) -- 129meg)
  3. Working With Pointers, Vectors, Matrices, and Strings

    Suggested Readings

  4. Working With Data Structures

    Suggested Readings

  5. Writing Research Programs

    Handout for Topic 5 (Part 1)
    Handout for Topic 5 (Part 2)
    Handout for Topic 5 (Part 3)
    Handout for Topic 5 (Part 4)
    Handout for Topic 5 (Part 5)

    Introductory Programs:

    read_dwnom_2.c -- Program that reads records of DW-NOMINATE legislator file as strings and then converts them to integers, reals, and character strings. This version features some nested sorting (courtesy of Jeff Lewis and Keith Poole).
    read_dwnom_3.c -- Program that reads records of DW-NOMINATE legislator file as strings and then converts them to integers, reals, and character strings. This version features some nested sorting (courtesy of Jeff Lewis and Keith Poole).
    read_dwnom_3A.c -- Same as above only slightly different code that produces a Compiler warning.
    hl01111b21.dat DW-NOMINATE legislator Coordinate File read by read_dwnom.c programs above.
    xamoeba_double.c -- Program that implements the Nelder-Mead method of minimizing an objective function in a multidimensional space. Solves Bessel Function of type J0.
    xamoeba_general_a.c -- Program that implements the Nelder-Mead method of minimizing an objective function in a multidimensional space. This version set up to solve OLS problem. Program reads data_ols.txt -- 1000 by 25 X matrix and Y vector -- created by writedata_ols_erf.c.
    xamoeba_test.c -- Program that implements the Nelder-Mead method of minimizing an objective function in a multidimensional space. This version set up to solve OLS problem. Program reads data_ols.txt -- 1000 by 25 X matrix and Y vector -- created by writedata_ols_erf.c. This is a simpler implementation than xamoeba_general.c.
    xamoeba_general_bush.c -- Amoeba program that implements the Nelder-Mead minimization method set up to solve an OLS problem. Reads Bush2000.txt.
    xamoeba_general_gore.c -- Amoeba program that implements the Nelder-Mead minimization method set up to solve an OLS problem. Reads Gore2000.txt.

    xamoeba_test_bush_probit.c -- Amoeba program that implements the Nelder-Mead minimization method set up to solve a PROBIT problem. Reads Bush2000.txt and sets the dependent variable = 1 if the Bush vote is >= 50%.
    xamoeba_test_gore_probit.c -- Amoeba program that implements the Nelder-Mead minimization method set up to solve a PROBIT problem. Reads Gore2000.txt and sets the dependent variable =1 if the Gore vote is >= 50%.

    xpowell_test.c -- Program that uses Powell's Quadratically Convergent Method of minimizing an objective function in a multidimensional space. This version set up to solve OLS problem. Program reads data_ols.txt -- 1000 by 25 X matrix and Y vector -- created by writedata_ols_erf.c.
    xpowell_test_bush.c -- Program that uses Powell's Quadratically Convergent Method of minimizing an objective function in a multidimensional space. This version set up to solve OLS problem. Reads Bush2000.txt.
    xpowell_test_gore.c -- Program that uses Powell's Quadratically Convergent Method of minimizing an objective function in a multidimensional space. This version set up to solve OLS problem. Reads Gore2000.txt.

    xamoeba_xpowell_probit.c -- Program that uses both Amoeba and Powell's Quadratically Convergent Method of minimizing an objective function in a multidimensional space. It also finds best solution using both algorithms and computes numerical first and second derivatives. This version set up to solve Probit problem. Reads Bush2000.txt.

    optimization_machine_probit_bush106.c -- Program that uses both Amoeba and Powell's Quadratically Convergent Method of minimizing an objective function in a multidimensional space. It also finds best solution using both algorithms and computes numerical first and second derivatives. It also calculates t-statistics and p-values for the estimated parameters. This version set up to solve Probit problem. Reads h106_example_2.raw.
    optimization_machine_logit_bush106.c -- Program that uses both Amoeba and Powell's Quadratically Convergent Method of minimizing an objective function in a multidimensional space. It also finds best solution using both algorithms and computes numerical first and second derivatives. It also calculates t-statistics and p-values for the estimated parameters. This version set up to solve Logit problem. Reads h106_example_2.raw.
    h106_example_2.raw.dat File With Dichotomous Dependent Variable and 7 Independent Variables read by the Probit and Logit Programs above.
    Logit Probability Derivation (PDF)
    junk_probit_2.c -- Preliminary test of Ordered Probit (Cut Points Fixed!!)
    1968nesparty.txt -- 1968 NES Party ID and Demographic Variables. Party ID is 0,1,2,3,4,5,6 and is used as the dependent variable
    junk_oprobit_test_2.c -- Uses Indicator Variables to Pick up Cut Points (Not final solution to the problem!)
    oprobit_big_3.srt -- 1968 NES Party ID and Demographic Variables. Data is stacked to create dichotomous dependent variable with indicators to pick up the cut points.
    optimization_machine_MDS.c -- Does classical Metric Multidimensional Scaling (Illustrated with famous Color Circle Data)
    color_circle_2.txt -- Similarities Data from Ekman (1954) color experiments.
    color_circle.r -- R program to plot estimate points from optimization_machine_MDS.c.
    data_optim_3.txt -- Subset of output from optimization_machine_MDS.c -- all the solutions as rows that are contained in output file data_optim_2.txt
    optimization_machine_king.c -- Analyzes King's English Votes and Seat Share Data for England
    king_data_2.txt -- King's Votes and Seat Share Data for England
    arms_test.c -- Program by Wally Gilks that does Adaptive Rejection Metropolis Sampling -- Simple setup to sample from a Normal Distribution
    arms_output_normal.r -- R program to plot sample drawn from Normal Distribution by arms_test.c.
    arms.out01 -- 100,000 draws from Normal Distribution from arms_test.c
    arms_test_ols.c -- Program by Wally Gilks that does Adaptive Rejection Metropolis Sampling -- Reads data_ols.txt and does arms on the 25 Betas and writes the chains to disk in the file data_arms_chains.txt
    arms_ols_chains_3.r -- R program that uses coda package to produce history graphs of the three sets of chains and computes the Gelman-Rubin statistic for convergence of multiple chains. Reads the output files from multiple runs (renamed data_arms_chains.txt files
    data_arms_chains_1.txt -- 10,000 draws from 25-dimensional parameter space (25 Betas of Regression) from arms_test_ols.c
    data_arms_chains_2.txt -- 10,000 draws from 25-dimensional parameter space (25 Betas of Regression) from arms_test_ols.c
    data_arms_chains_3.txt -- 10,000 draws from 25-dimensional parameter space (25 Betas of Regression) from arms_test_ols.c
    arms_test_king.c -- Program by Wally Gilks that does Adaptive Rejection Metropolis Sampling -- Reads king_data_2.txt and does arms on the 2 Bias Parameters and the Responsiveness parameter and writes the chains to disk in the file data_arms_chains_king.txt. Note that I have placed priors in the keithrules() plug-in routine so this is a true Bayesian Posterior. I used an Exponential Prior for the Responsiveness Parameter and Normal vague priors for the two bias parameters.
    arms_king_chains_3.r -- R program that uses coda package to produce history graphs of the three sets of chains and computes the Gelman-Rubin statistic for convergence of multiple chains. Reads the output files from multiple runs (renamed data_arms_chains_king_Bayes_*.txt files
    data_arms_chains_King_Bayes_1.txt -- 10,000 draws from 3-dimensional parameter space (two bias parameters and a responsiveness parameter) from arms_test_king.c
    data_arms_chains_King_Bayes_2.txt -- 10,000 draws from 3-dimensional parameter space (two bias parameters and a responsiveness parameter) from arms_test_king.c
    data_arms_chains_King_Bayes_3.txt -- 10,000 draws from 3-dimensional parameter space (two bias parameters and a responsiveness parameter) from arms_test_king.c
    arms_test_probit.c -- Program by Wally Gilks that does Adaptive Rejection Metropolis Sampling -- Reads h106_example_2.raw and does probit on the 7 congressional district level variables for the 2000 election. The dependent variable = 1 if Bush won the district. Note that I have placed priors in the keithrules() plug-in routine so this is a true Bayesian Posterior. I used Normal vague priors for the eight beta coefficients in the probit (intercept term plus the seven variables).
    arms_probit_chains_3.r -- R program that uses coda package to produce history graphs of the three sets of chains and computes the Gelman-Rubin statistic for convergence of multiple chains. Reads the output files from multiple runs (renamed data_arms_chains_probit_*.txt files
    data_arms_chains_probit_1.txt -- 10,000 draws from 8-dimensional parameter space (eight betas) from arms_test_probit.c
    data_arms_chains_probit_2.txt -- 10,000 draws from 8-dimensional parameter space (eight betas) from arms_test_probit.c
    data_arms_chains_probit_3.txt -- 10,000 draws from 8-dimensional parameter space (eight betas) from arms_test_probit.c
    arms_test_logit.c -- Program by Wally Gilks that does Adaptive Rejection Metropolis Sampling -- Reads h106_example_2.raw and does logit on the 7 congressional district level variables for the 2000 election. The dependent variable = 1 if Bush won the district. Note that I have placed priors in the keithrules() plug-in routine so this is a true Bayesian Posterior. I used Normal vague priors for the eight beta coefficients in the probit (intercept term plus the seven variables).
    arms_logit_chains_3.r -- R program that uses coda package to produce history graphs of the three sets of chains and computes the Gelman-Rubin statistic for convergence of multiple chains. Reads the output files from multiple runs (renamed data_arms_chains_logit_*.txt files
    data_arms_chains_logit_1.txt -- 10,000 draws from 8-dimensional parameter space (eight betas) from arms_test_probit.c
    data_arms_chains_logit_2.txt -- 10,000 draws from 8-dimensional parameter space (eight betas) from arms_test_probit.c
    data_arms_chains_logit_3.txt -- 10,000 draws from 8-dimensional parameter space (eight betas) from arms_test_probit.c
    Day_Ten_Sept_25_2009.mp3 (MP3 file for tenth two hours -- 128meg)
    Day_Eleven_Oct_9_2009.mp3 (MP3 file for eleventh two hours -- 136meg)
    Day_Twelve_Oct_23_2009.mp3 (MP3 file for twelfth two hours -- 111meg)
    Day_Thirteen_Dec_4_2009.mp3 (MP3 file for thirteenth two hours -- 108meg)


  6. Additional Research Programs (updated 25 April 2012)

    normaltable.c -- Program that computes normal distribution look-up table from -5.0 to +5.0 in steps of 0.0001. Table is written out in normaltable.dat. The table has 5 columns, a counter, the axis value, F(x), ln(F(x)), and f(x)/F(x) (the inverse of the Mills Ratio).

    qrmethod_2.c -- Program that performs QR Decomposition using the LAPACK and BLAS packages -- Reads experiment_qr_3.txt and does QR Decomposition.



Site Links

VOTEVIEW Blog
NOMINATE Data, Roll Call Data, and Software
Course Web Pages: University of Georgia (2010 - )
Course Web Pages: UC San Diego (2004 - 2010)
University of San Diego Law School (2005)
Course Web Pages: University of Houston (2000 - 2005)
Course Web Pages: Carnegie-Mellon University (1997 - 2000)
Analyzing Spatial Models of Choice and Judgment with R
Spatial Models of Parliamentary Voting
Recent Working Papers
Analyses of Recent Politics
About This Website
K7MOA Log Books: 1960 - 2015
Bio of Keith T. Poole
Related Links