# use this library for constructing the
# log-likelihood and its gradient and Hessian
library(RegressionFactory)
# simulate data for logistic regression
N <- 1000 # number of observations
K <- 5 # number of attributes
X <- matrix(runif(N*K, min = -0.5, max = +0.5), ncol = K)
beta <- runif(K, min = -0.5, max = +0.5)
y <- 1*(runif(N) < 1/(1 + exp(- X %*% beta)))
# define log-likelihood using expander framework of
# RegressionFactory package
loglike <- function(beta, X, y) {
regfac.expand.1par(beta, X, y, fbase1.binomial.logit, fgh = 2, n = 1)
}
# SNS sampling
nsmp <- 100
beta.init <- rep(0.0, K)
sns.out <- sns.run(beta.init, loglike, 200, X = X, y = y)
summary(sns.out)
Run the code above in your browser using DataLab