Next: About this document ...
Up: nesR
Previous: Data program example:
*** RESULTS YOUR PROGRAM PRODUCED ***
R : Copyright 2005, The R Foundation for Statistical Computing
Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for a HTML browser interface to help.
Type 'q()' to quit R.
> invisible(options(echo = TRUE))
> # R
> options(echo=FALSE);
> # using data from year 2004
>
> # VCF0301 is 7-pt Scale Party Identification
> # VCF0704 is Party of Pres Vote- All Major Candi
> # VCF0704A is Party of Pres Vote- 2 Major Parties
> # VCF0724 is Did R Watch TV Programs about Campa
> # VCF0803 is Liberal-Conservative 7pt Scale
> # VCF9088 is Dem Pres Cand- 7pt Liberal-Conserv
> # VCF9096 is Rep Pres Cand- 7pt Liberal-Conserv
> DF <- read.table(file="tmp.sas2awk.dat",header=TRUE);
> attach(DF);
> # preserve raw variables
> oVCF0301 <- VCF0301;
> oVCF0704 <- VCF0704;
> oVCF0704A <- VCF0704A;
> oVCF0724 <- VCF0724;
> oVCF0803 <- VCF0803;
> oVCF9088 <- VCF9088;
> oVCF9096 <- VCF9096;
> # set NES-defined missing values to R's NA code
>
> VCF0301 <- ifelse( VCF0301 == 0, NA, VCF0301 );
> VCF0704 <- ifelse( VCF0704 == 0, NA, VCF0704 );
> VCF0704A <- ifelse( VCF0704A == 0, NA, VCF0704A );
> VCF0724 <- ifelse( VCF0724 == 0, NA, VCF0724 );
> VCF0803 <- ifelse( VCF0803 == 0, NA, VCF0803 );
> VCF9088 <- ifelse( VCF9088 == 0, NA, VCF9088 );
> VCF9088 <- ifelse( VCF9088 >= 8, NA, VCF9088 );
> VCF9096 <- ifelse( VCF9096 == 0, NA, VCF9096 );
> VCF9096 <- ifelse( VCF9096 >= 8, NA, VCF9096 );
>
> # get rid of unwanted value in party ID
> pid <- VCF0301;
> pid <- ifelse(pid == 9, NA, pid);
>
> # get rid of unwanted value in libcon self-placement
> libcon <- VCF0803;
> libcon <- ifelse(libcon == 9, NA, libcon);
>
> # presvote3 is three major party candidates only
> presvote3 <- VCF0704;
>
> # presvote is Dem and Rep candidates only
> presvote <- VCF0704A - 1;
>
> # watchtv is how much watched campaign on TV
> watchtv <- VCF0724;
>
> # compute spatial model measure of difference between Dem and Rep candidates
> demlibcon <- VCF9088;
> replibcon <- VCF9096;
> demdist <- abs(libcon-demlibcon);
> repdist <- abs(libcon-replibcon);
> distdiff <- demdist-repdist;
>
> # crosstab of libcon given pid
> # xtable libcon pid
> if (!XallmissXXX(all(is.na(libcon)) | all(is.na(pid)) )) {
+ XtableXXX <- XruntabXXX(as.formula("wgtXXX ~ libcon + pid"), 2, "Y");
+ }
[1] "weighted frequencies "
pid
libcon 1 2 3 4 5 6 7
1 7 4 9 0 0 0 0
2 51 19 26 0 2 3 1
3 31 24 43 7 11 7 1
4 43 46 54 35 32 39 24
5 4 15 8 14 30 41 31
6 3 5 7 3 26 39 83
7 1 2 1 1 5 1 19
[1] "independence test "
Call: xtabs(formula = f, drop.unused.levels = T)
Number of cases in table: 858
Number of factors: 2
Test for independence of all factors:
Chisq = 491.8, df = 36, p-value = 2.17e-81
Chi-squared approximation may be incorrect
[1] "weighted column proportions "
pid
libcon 1 2 3 4 5 6 7
1 0.050 0.035 0.061 0.000 0.000 0.000 0.000
2 0.364 0.165 0.176 0.000 0.019 0.023 0.006
3 0.221 0.209 0.291 0.117 0.104 0.054 0.006
4 0.307 0.400 0.365 0.583 0.302 0.300 0.151
5 0.029 0.130 0.054 0.233 0.283 0.315 0.195
6 0.021 0.043 0.047 0.050 0.245 0.300 0.522
7 0.007 0.017 0.007 0.017 0.047 0.008 0.119
>
> # crosstab of libcon given pid and watchtv
> # xtable libcon pid watchtv
> if (!XallmissXXX(all(is.na(libcon)) | all(is.na(pid)) | all(is.na(watchtv)) )) {
+ XtableXXX <- XruntabXXX(as.formula("wgtXXX ~ libcon + pid + watchtv"), 3, "Y");
+ }
[1] "weighted frequencies "
, , watchtv = 1
pid
libcon 1 2 3 4 5 6 7
1 2 0 1 0 0 0 0
2 0 2 4 0 0 2 0
3 3 1 4 2 1 2 0
4 6 10 6 6 3 5 3
5 0 1 0 0 4 5 3
6 0 0 0 0 1 8 5
7 0 2 0 1 1 1 3
, , watchtv = 2
pid
libcon 1 2 3 4 5 6 7
1 5 4 8 0 0 0 0
2 51 17 22 0 2 1 1
3 28 23 39 5 10 5 1
4 37 36 48 29 29 34 21
5 4 14 8 14 26 36 28
6 3 5 7 3 25 31 78
7 1 0 1 0 4 0 16
[1] "weighted column proportions "
, , watchtv = 1
pid
libcon 1 2 3 4 5 6 7
1 0.182 0.000 0.067 0.000 0.000 0.000 0.000
2 0.000 0.125 0.267 0.000 0.000 0.087 0.000
3 0.273 0.062 0.267 0.222 0.100 0.087 0.000
4 0.545 0.625 0.400 0.667 0.300 0.217 0.214
5 0.000 0.062 0.000 0.000 0.400 0.217 0.214
6 0.000 0.000 0.000 0.000 0.100 0.348 0.357
7 0.000 0.125 0.000 0.111 0.100 0.043 0.214
, , watchtv = 2
pid
libcon 1 2 3 4 5 6 7
1 0.039 0.040 0.060 0.000 0.000 0.000 0.000
2 0.395 0.172 0.165 0.000 0.021 0.009 0.007
3 0.217 0.232 0.293 0.098 0.104 0.047 0.007
4 0.287 0.364 0.361 0.569 0.302 0.318 0.145
5 0.031 0.141 0.060 0.275 0.271 0.336 0.193
6 0.023 0.051 0.053 0.059 0.260 0.290 0.538
7 0.008 0.000 0.008 0.000 0.042 0.000 0.110
>
> # crosstab of three-party vote given pid and watchtv
> # xtable presvote3 pid
> if (!XallmissXXX(all(is.na(presvote3)) | all(is.na(pid)) )) {
+ XtableXXX <- XruntabXXX(as.formula("wgtXXX ~ presvote3 + pid"), 2, "Y");
+ }
[1] "weighted frequencies "
pid
presvote3 1 2 3 4 5 6 7
1 148 92 108 24 9 13 3
2 5 19 16 21 73 108 165
3 0 0 0 1 3 0 0
[1] "independence test "
Call: xtabs(formula = f, drop.unused.levels = T)
Number of cases in table: 808
Number of factors: 2
Test for independence of all factors:
Chisq = 553.6, df = 12, p-value = 8.296e-111
Chi-squared approximation may be incorrect
[1] "weighted column proportions "
pid
presvote3 1 2 3 4 5 6 7
1 0.967 0.829 0.871 0.522 0.106 0.107 0.018
2 0.033 0.171 0.129 0.457 0.859 0.893 0.982
3 0.000 0.000 0.000 0.022 0.035 0.000 0.000
>
> # crosstab of two-party vote given pid and watchtv
> # xtable presvote pid
> if (!XallmissXXX(all(is.na(presvote)) | all(is.na(pid)) )) {
+ XtableXXX <- XruntabXXX(as.formula("wgtXXX ~ presvote + pid"), 2, "Y");
+ }
[1] "weighted frequencies "
pid
presvote 1 2 3 4 5 6 7
0 148 92 108 24 9 13 3
1 5 19 16 21 73 108 165
[1] "independence test "
Call: xtabs(formula = f, drop.unused.levels = T)
Number of cases in table: 804
Number of factors: 2
Test for independence of all factors:
Chisq = 530.8, df = 6, p-value = 1.913e-111
[1] "weighted column proportions "
pid
presvote 1 2 3 4 5 6 7
0 0.967 0.829 0.871 0.533 0.110 0.107 0.018
1 0.033 0.171 0.129 0.467 0.890 0.893 0.982
>
> # one-way frequency table of libcon placements
> # xtable libcon
> if (!XallmissXXX(all(is.na(libcon)) )) {
+ XtableXXX <- XruntabXXX(as.formula("wgtXXX ~ libcon"), 1, "Y");
+ }
[1] "weighted frequencies "
libcon
1 2 3 4 5 6 7
20 103 125 279 143 166 31
[1] "weighted proportions "
libcon
1 2 3 4 5 6 7
0.023 0.119 0.144 0.322 0.165 0.191 0.036
> # xtable demlibcon
> if (!XallmissXXX(all(is.na(demlibcon)) )) {
+ XtableXXX <- XruntabXXX(as.formula("wgtXXX ~ demlibcon"), 1, "Y");
+ }
[1] "weighted frequencies "
demlibcon
1 2 3 4 5 6 7
166 317 233 215 78 52 27
[1] "weighted proportions "
demlibcon
1 2 3 4 5 6 7
0.153 0.291 0.214 0.198 0.072 0.048 0.025
> # xtable replibcon
> if (!XallmissXXX(all(is.na(replibcon)) )) {
+ XtableXXX <- XruntabXXX(as.formula("wgtXXX ~ replibcon"), 1, "Y");
+ }
[1] "weighted frequencies "
replibcon
1 2 3 4 5 6 7
57 71 65 111 134 412 234
[1] "weighted proportions "
replibcon
1 2 3 4 5 6 7
0.053 0.065 0.060 0.102 0.124 0.380 0.216
>
> # simple ordinary least squares regression model of two-party vote
> # explanatory variables are PID dummy variables and spatial model difference
> summary(lm(presvote ~ factor(pid) + distdiff, weights=wgtXXX));
Call:
lm(formula = presvote ~ factor(pid) + distdiff, weights = wgtXXX)
Residuals:
Min 1Q Median 3Q Max
-1.044864 -0.089015 -0.007324 0.077937 1.074544
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.130125 0.029737 4.376 1.41e-05 ***
factor(pid)2 0.122625 0.040432 3.033 0.00252 **
factor(pid)3 0.069647 0.037328 1.866 0.06253 .
factor(pid)4 0.395065 0.059591 6.630 7.22e-11 ***
factor(pid)5 0.706136 0.047401 14.897 < 2e-16 ***
factor(pid)6 0.710070 0.043107 16.472 < 2e-16 ***
factor(pid)7 0.725409 0.047389 15.307 < 2e-16 ***
distdiff 0.040934 0.005627 7.275 1.03e-12 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.268 on 633 degrees of freedom
Multiple R-Squared: 0.714, Adjusted R-squared: 0.7108
F-statistic: 225.7 on 7 and 633 DF, p-value: < 2.2e-16
>
> # simple probit regression model of two-party vote
> # explanatory variables are PID dummy variables and spatial model difference
> summary(glm(presvote ~ factor(pid) + distdiff,
+ family=binomial(link="probit"), weights=wgtXXX));
Call:
glm(formula = presvote ~ factor(pid) + distdiff, family = binomial(link = "probit"),
weights = wgtXXX)
Deviance Residuals:
Min 1Q Median 3Q Max
-3.13986 -0.32572 0.04461 0.24418 3.54923
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.51587 0.29711 -5.102 3.36e-07 ***
factor(pid)2 0.93058 0.34264 2.716 0.00661 **
factor(pid)3 0.72013 0.34312 2.099 0.03583 *
factor(pid)4 1.67495 0.38864 4.310 1.63e-05 ***
factor(pid)5 2.61418 0.37399 6.990 2.75e-12 ***
factor(pid)6 2.57278 0.34767 7.400 1.36e-13 ***
factor(pid)7 3.21911 0.44820 7.182 6.86e-13 ***
distdiff 0.27773 0.04354 6.379 1.79e-10 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 883.18 on 640 degrees of freedom
Residual deviance: 295.96 on 633 degrees of freedom
AIC: 311.96
Number of Fisher Scoring iterations: 7
>
> proc.time()
[1] 0.86 0.12 1.16 0.01 0.00
>
Walter Mebane
2005-11-03