9 if(!exists("top.ngram.matrix")){
10 load("processed_data/top.ngram.matrix.RData")
13 if(!exists("pred.descrip")){
14 load("paper/data/prediction_descriptives.RData")
15 covars <- pred.descrip$covars
18 top.ngram.matrix <- data.table(top.ngram.matrix)
19 setkey(top.ngram.matrix, eid)
20 covars <- data.table(pred.descrip$covars)
23 # restrict to the overlap of the two datasets
24 covars <- covars[covars$eid %in% top.ngram.matrix$eid,]
26 top.ngram.matrix <- top.ngram.matrix[top.ngram.matrix$eid %in%
29 # rename the cited column in case it doesn't appear
30 names(covars)[names(covars) == 'cited'] <- 'cited.x'
32 # then merge also to facilitate some manipulations below
33 d <- merge(covars, top.ngram.matrix, by="eid", all=FALSE)
35 # Note that this duplicates some column names so X gets appended in a
38 # construct model matrices
39 x.controls <- sparse.model.matrix(cited.x ~ language.x +
40 modal_country + month.x,
43 x.aff <- sparse.model.matrix(cited.x ~ affiliation, data=d)[,-1]
44 x.subj <- sparse.model.matrix(cited.x ~ subject.x, data=d)[,-1]
45 x.venue <- sparse.model.matrix(cited.x ~ source_title, data=d)[,-1]
47 x.ngrams <- as.matrix(subset(top.ngram.matrix, select=-eid))
48 x.ngrams <- as(x.ngrams, "sparseMatrix")
50 X <- cBind(x.controls, covars$year.x, covars$works.cited)
51 X.aff <- cBind(X, x.aff)
52 X.subj <- cBind(X.aff, x.subj)
53 X.venue <- cBind(X.subj, x.venue)
54 X.terms <- cBind(X.venue, x.ngrams)
58 ### Hold-back sample for testing model performance later on:
60 holdback.index <- sample(nrow(X), round(nrow(X)*.1))
62 X.hold <- X[holdback.index,]
63 X.hold.aff <- X.aff[holdback.index,]
64 X.hold.subj <- X.subj[holdback.index,]
65 X.hold.venue <- X.venue[holdback.index,]
66 X.hold.terms <- X.terms[holdback.index,]
67 Y.hold <- Y[holdback.index]
69 X.test <- X[-holdback.index,]
70 X.test.aff <- X.aff[-holdback.index,]
71 X.test.subj <- X.subj[-holdback.index,]
72 X.test.venue <- X.venue[-holdback.index,]
73 X.test.terms <- X.terms[-holdback.index,]
74 Y.test <- Y[-holdback.index]
76 ############### Models and prediction
80 m.con <- cv.glmnet(X.test, Y.test, alpha=1, family="binomial",
82 con.pred = predict(m.con, type="class", s="lambda.min",
85 m.aff <- cv.glmnet(X.test.aff, Y.test, alpha=1, family="binomial",
87 aff.pred = predict(m.aff, type="class", s="lambda.min",
90 m.subj <- cv.glmnet(X.test.subj, Y.test, alpha=1, family="binomial",
92 subj.pred = predict(m.subj, type="class", s="lambda.min",
95 m.venue <- cv.glmnet(X.test.venue, Y.test, alpha=1, family="binomial",
97 venue.pred = predict(m.venue, type="class", s="lambda.min",
100 m.terms <- cv.glmnet(X.test.terms, Y.test, alpha=1, family="binomial",
101 type.measure="class")
102 terms.pred = predict(m.terms, type="class", s="lambda.min",
106 # Compare test set predictions against held-back sample:
108 pred.df <- data.frame(cbind(con.pred, aff.pred, subj.pred,
109 venue.pred, terms.pred))
110 names(pred.df) <- c("Controls", "+ Affiliation", "+ Subject", "+ Venue",
113 m.list <- list(m.con, m.aff, m.subj, m.venue, m.terms)
118 # nonzero coefficients
121 gen.m.summ.info <- function(model){
122 df <- round(tail(model$glmnet.fit$df, 1),0)
123 percent.dev <- round(tail(model$glmnet.fit$dev.ratio, 1),2)*100
124 cv.error <- round(tail(model$cvm,1),2)*100
125 # null.dev <- round(tail(model$glmnet.fit$nulldev),0)
126 out <- c(df, percent.dev, cv.error)
130 gen.class.err <- function(pred, test){
131 props <- prop.table(table(pred, test))
132 err.sum <- round(sum(props[1,2], props[2,1]),2)*100
137 results.tab <- cbind(names(pred.df),data.frame(matrix(unlist(lapply(m.list,
141 results.tab$class.err <- sapply(pred.df, function(x) gen.class.err(x,
144 results.tab <- data.frame(lapply(results.tab, as.character))
148 names(results.tab) <- c("Model", "N features", "Deviance (%)",
149 "CV error (%)", "Hold-back error (%)")
152 print(xtable(results.tab,
154 "Summary of fitted models predicting any citations. The ``Model'' column describes which features were included. The N features column shows the number of features included in the prediction. ``Deviance'' summarizes the goodness of fit as a percentage of the total deviance accounted for by the model. ``CV error'' (cross-validation error) reports the prediction error rates of each model in the cross-validation procedure conducted as part of the parameter estimation process. ``Hold-back error'' shows the prediction error on a random 10 percent subset of the original dataset not included in any of the model estimation procedures.",
155 label='tab:predict_models', align='llrrrr'),
156 include.rownames=FALSE)
159 predict.list$results.tab <- results.tab
164 ############# Generate most salient coefficients
165 nz.coefs <- data.frame( coef =
166 colnames(X.test.terms)[which(
167 coef(m.terms, s="lambda.min")
172 s="lambda.min")[which(coef(m.terms,
176 nz.coefs$coef <- as.character(nz.coefs$coef)
177 nz.coefs$type <- as.character(nz.coefs$type)
178 nz.coefs <- nz.coefs[order(-abs(nz.coefs$beta)),]
182 #nz.coefs$type <- "terms"
183 nz.coefs$type[grepl("(Intercept)", nz.coefs$coef)] <- NA
184 nz.coefs$type[grepl("source_title", nz.coefs$coef)] <- "venue"
185 nz.coefs$type[grepl("subject.x", nz.coefs$coef)] <- "subject"
186 nz.coefs$type[grepl("affiliation", nz.coefs$coef)] <- "affiliation"
187 nz.coefs$type[grepl("month.x", nz.coefs$coef)] <- "month"
188 nz.coefs$type[grepl("modal_country", nz.coefs$coef)] <- "country"
189 nz.coefs$type[grepl("language", nz.coefs$coef)] <- "language"
190 nz.coefs$type[grepl("^20[0-9]{2}$", nz.coefs$coef)] <- "year"
194 nz.coefs$coef <- gsub("source_title", "", nz.coefs$coef)
195 nz.coefs$coef <- gsub("subject.x", "", nz.coefs$coef)
196 nz.coefs$coef <- gsub("affiliation","", nz.coefs$coef)
197 nz.coefs$beta <- round(nz.coefs$beta, 3)
198 names(nz.coefs) <- c("Feature", "Type", "Coefficient")
200 predict.list$nz.coefs <- nz.coefs
203 round(prop.table(table(nz.coefs$Type))*100, 2)
206 round(prop.table(table(nz.coefs$Type[1:700]))*100, 2)
207 round(prop.table(table(nz.coefs$Type[1:200]))*100, 2)
208 round(prop.table(table(nz.coefs$Type[1:100]))*100, 2)
211 as.matrix(head(nz.coefs, 10)),
213 caption='Feature, variable type, and beta value for top 100 non-zero coefficients estimated by the best fitting model with all features included.',
215 ), include.rownames=FALSE)
219 save(predict.list, file="paper/data/prediction.RData")