SciencesPo by Daniel Marcelino

Tools for analyzing political behaviour data

See http://danielmarcelino.com/tag/sciencespo/

SciencePo is a package facility dedicated to political scientists. The very objective of the package is to cover standard analyses for random and nonrandom data. Later updates will focus on implementing analytical advancements published in relevant journals of the field.

require(SciencesPo)
## Loading required package: SciencesPo
## Loading required package: data.table

Make a data frame:

weather <- data.frame(
  month = a <- c('J','F','M','A','M','J','J','A','S','O','N','D'),
  precip = b <- c(78,62,74,78,76, 83, 91, 93, 93,78, 93, 81),
  max = c <- c(-6, -4, 2, 11, 19, 24, 26, 25, 20, 13, 5, -2),
  min = d <- c(-15, -13, -7, 1, 8, 13, 16, 14, 9, 3, -2, -10))

weather
##    month precip max min
## 1      J     78  -6 -15
## 2      F     62  -4 -13
## 3      M     74   2  -7
## 4      A     78  11   1
## 5      M     76  19   8
## 6      J     83  24  13
## 7      J     91  26  16
## 8      A     93  25  14
## 9      S     93  20   9
## 10     O     78  13   3
## 11     N     93   5  -2
## 12     D     81  -2 -10

Anonymize it, scrambling both factors and numbers:

anonymize(weather)
##    X1                 X2                  X3                 X4
## 1  c4  -3.66666666666667   -17.0833333333333  -16.4166666666667
## 2  c3  -19.6666666666667   -15.0833333333333  -14.4166666666667
## 3  c5  -7.66666666666667   -9.08333333333333  -8.41666666666667
## 4  c1  -3.66666666666667 -0.0833333333333339 -0.416666666666667
## 5  c5  -5.66666666666667    7.91666666666667   6.58333333333333
## 6  c4   1.33333333333333    12.9166666666667   11.5833333333333
## 7  c4   9.33333333333333    14.9166666666667   14.5833333333333
## 8  c1   11.3333333333333    13.9166666666667   12.5833333333333
## 9  c8   11.3333333333333    8.91666666666667   7.58333333333333
## 10 c7  -3.66666666666667    1.91666666666667   1.58333333333333
## 11 c6   11.3333333333333   -6.08333333333333  -3.41666666666667
## 12 c2 -0.666666666666671   -13.0833333333333  -11.4166666666667

Is it possible to anonymise one variable, within a data frame, or data.table?

anonymize(weather[1])
##    sapply.seq_along.x...level.obs.
## 1                               a4
## 2                               a3
## 3                               a5
## 4                               a1
## 5                               a5
## 6                               a4
## 7                               a4
## 8                               a1
## 9                               a8
## 10                              a7
## 11                              a6
## 12                              a2
weather$anon <- anonymize(weather[1])
str(weather)
## 'data.frame':    12 obs. of  5 variables:
##  $ month : Factor w/ 8 levels "A","D","F","J",..: 4 3 5 1 5 4 4 1 8 7 ...
##  $ precip: num  78 62 74 78 76 83 91 93 93 78 ...
##  $ max   : num  -6 -4 2 11 19 24 26 25 20 13 ...
##  $ min   : num  -15 -13 -7 1 8 13 16 14 9 3 ...
##  $ anon  :'data.frame':  12 obs. of  1 variable:
##   ..$ sapply.seq_along.x...level.obs.: Factor w/ 8 levels "a1","a2","a3",..: 4 3 5 1 5 4 4 1 8 7 ...

attr(weather[, ncol(weather)], "names") <- "anon"
str(weather)
## 'data.frame':    12 obs. of  5 variables:
##  $ month : Factor w/ 8 levels "A","D","F","J",..: 4 3 5 1 5 4 4 1 8 7 ...
##  $ precip: num  78 62 74 78 76 83 91 93 93 78 ...
##  $ max   : num  -6 -4 2 11 19 24 26 25 20 13 ...
##  $ min   : num  -15 -13 -7 1 8 13 16 14 9 3 ...
##  $ anon  : Factor w/ 8 levels "a1","a2","a3",..: 4 3 5 1 5 4 4 1 8 7 ...

The function:

anonymize
## function (x) 
## {
##     if (length(x) > 26) {
##     }
##     names(x) <- paste(sample(letters[1:length(x)]))
##     level.x <- function(x) {
##         level.obs <- function(i) {
##             if (class(x[, i]) == "factor" | class(x[, i]) == 
##                 "character") {
##                 var <- paste(names(x)[i], as.numeric(as.factor(x[, 
##                   i])), sep = "")
##             }
##             else if (is.numeric(x[, i])) {
##                 var <- x[, i] - mean(x[, i], na.rm = T)
##             }
##             else {
##                 var <- x[, i]
##             }
##             return(var)
##         }
##         x <- data.frame(sapply(seq_along(x), level.obs))
##         names(x) <- names(x)
##         return(x)
##     }
##     x <- level.x(x)
##     return(x)
## }
## <bytecode: 0x00000000084ae038>
## <environment: namespace:SciencesPo>

Detail a data frame:

detail(weather)
##          vars obs mean   sd   var  se median  mad trimmed winsor range min
## month !#    1  12  4.2  2.2   4.9 0.6    4.0  2.2     4.1    4.1     7   1
## precip      2  12 81.7  9.5  90.2 2.7   79.5  6.7    82.2   82.7    31  62
## max         3  12 11.1 11.8 139.9 3.4   12.0 16.3    11.5   11.2    32  -6
## min         4  12  1.4 10.9 118.1 3.1    2.0 14.8     1.9    1.4    31 -15
## anon !#     5  12  4.2  2.2   4.9 0.6    4.0  2.2     4.1    4.1     7   1
##          max skew kurt
## month !#   8  0.1 -0.6
## precip    93 -0.4  0.1
## max       26 -0.1 -1.6
## min       16 -0.2 -1.4
## anon !#    8  0.1 -0.6
## First Part Obtaining some data
require(foreign)
## Loading required package: foreign
seniors <- read.dta("http://www.stata-press.com/data/r10/seniors.dta")

wave1 <- subset(seniors, select = -c(race, county))
wave2 <- subset(seniors, select = -c(gender, smoked, county))
wave3 <- subset(seniors, select = -c(id, school, county))
wave4 <- subset(seniors, select = -c(state, height))
wave5 <- subset(seniors, select = -c(county, sampwgt))

## Second Part Getting information of data Creating empty objects
df.a <- list()
df.b <- list()
df.c <- list()
df.d <- list()
df.e <- list()

## df.a[[i]] throws an error -- example taken from within a loop?

df.a[[1]] <- data.frame(name = names(wave1), wave1 = "yes")
df.b[[1]] <- data.frame(name = names(wave2), wave2 = "yes")
df.c[[1]] <- data.frame(name = names(wave3), wave3 = "yes")
df.d[[1]] <- data.frame(name = names(wave4), wave4 = "yes")
df.e[[1]] <- data.frame(name = names(wave5), wave5 = "yes")

df1 <- merge(df.e, df.d, all = TRUE)
df2 <- merge(df1, df.c, all = TRUE)
df3 <- merge(df2, df.b, all = TRUE)
outfile <- merge(df3, df.a, all = TRUE)

# Table to display the results
print(outfile)
##         name wave5 wave4 wave3 wave2 wave1
## 1     gender   yes   yes   yes  <NA>   yes
## 2     height   yes  <NA>   yes   yes   yes
## 3         id   yes   yes  <NA>   yes   yes
## 4  ncounties   yes   yes   yes   yes   yes
## 5   nschools   yes   yes   yes   yes   yes
## 6   nseniors   yes   yes   yes   yes   yes
## 7       race   yes   yes   yes   yes  <NA>
## 8     school   yes   yes  <NA>   yes   yes
## 9     smoked   yes   yes   yes  <NA>   yes
## 10     state   yes  <NA>   yes   yes   yes
## 11    weight   yes   yes   yes   yes   yes
## 12    county  <NA>   yes  <NA>  <NA>  <NA>
## 13   sampwgt  <NA>   yes   yes   yes   yes
detail(outfile)
##          vars obs mean  sd  var  se median mad trimmed winsor range min
## name !#     1  13    7 3.9 15.2 1.1      7 4.4       7      7    12   1
## wave5 !#    2  11    1 0.0  0.0 0.0      1 0.0       1      1     0   1
## wave4 !#    3  11    1 0.0  0.0 0.0      1 0.0       1      1     0   1
## wave3 !#    4  10    1 0.0  0.0 0.0      1 0.0       1      1     0   1
## wave2 !#    5  10    1 0.0  0.0 0.0      1 0.0       1      1     0   1
## wave1 !#    6  11    1 0.0  0.0 0.0      1 0.0       1      1     0   1
##          max skew kurt
## name !#   13    0 -1.2
## wave5 !#   1  NaN  NaN
## wave4 !#   1  NaN  NaN
## wave3 !#   1  NaN  NaN
## wave2 !#   1  NaN  NaN
## wave1 !#   1  NaN  NaN
str(outfile)
## 'data.frame':    13 obs. of  6 variables:
##  $ name : Factor w/ 13 levels "gender","height",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ wave5: Factor w/ 1 level "yes": 1 1 1 1 1 1 1 1 1 1 ...
##  $ wave4: Factor w/ 1 level "yes": 1 NA 1 1 1 1 1 1 1 NA ...
##  $ wave3: Factor w/ 1 level "yes": 1 1 NA 1 1 1 1 NA 1 1 ...
##  $ wave2: Factor w/ 1 level "yes": NA 1 1 1 1 1 1 1 NA 1 ...
##  $ wave1: Factor w/ 1 level "yes": 1 1 1 1 1 1 NA 1 1 1 ...
summary(outfile)
##         name    wave5     wave4     wave3     wave2     wave1   
##  gender   :1   yes :11   yes :11   yes :10   yes :10   yes :11  
##  height   :1   NA's: 2   NA's: 2   NA's: 3   NA's: 3   NA's: 2  
##  id       :1                                                    
##  ncounties:1                                                    
##  nschools :1                                                    
##  nseniors :1                                                    
##  (Other)  :7