Skip to content

Instantly share code, notes, and snippets.

# Predict the test set using the model
pred_lasso = predict(glmmod, test_sparse, type="response", s=best.lambda)
pred_lasso
# Apply a threshold
new_pred_lasso = ifelse(pred_lasso >= 0.5, 1, 0)
new_pred_lasso = data.frame(new_pred_lasso)
data_lasso = cbind(test[,2], new_pred_lasso)
names(data_lasso) = c("actual", "pred")
xtab_lasso = table(data_lasso$actual, data_lasso$pred)
@joshkyh
joshkyh / analysis.r
Last active May 16, 2018 09:42
Different fit given the same r-square
# getBicop is a snippet from https://stats.stackexchange.com/questions/15011/generate-a-random-variable-with-a-defined-correlation-to-an-existing-variables/15035#15035
# returns a data frame of two variables which correlate with a population correlation of rho
# If desired, one of both variables can be fixed to an existing variable by specifying x
getBiCop <- function(n, rho, mar.fun=rnorm, x = NULL, ...) {
if (!is.null(x)) {X1 <- x} else {X1 <- mar.fun(n, ...)}
if (!is.null(x) & length(x) != n) warning("Variable x does not have the same length as n!")
C <- matrix(rho, nrow = 2, ncol = 2)
diag(C) <- 1
@joshkyh
joshkyh / word2vec_emotions.py
Last active January 30, 2018 10:23
word2vec_emotions.py
import gensim
from gensim.models.keyedvectors import KeyedVectors
# Load Google's pre-trained Word2Vec model.
model = KeyedVectors.load_word2vec_format('I:/Downloads/GoogleNews-vectors-negative300.bin/GoogleNews-vectors-negative300.bin', binary=True)
word1 = 'anger'
word2 = 'trust'
model.most_similar(positive = [word1, word2])
word_w2v = 'resentment'
import gensim
from gensim.models.keyedvectors import KeyedVectors
# Load Google's pre-trained Word2Vec model.
model = KeyedVectors.load_word2vec_format('I:/Downloads/GoogleNews-vectors-negative300.bin/GoogleNews-vectors-negative300.bin', binary=True)
word1 = 'anger'