################################################
# How to use the function "hansen()"           #
#                                              #################################
# Take a matrix, no dataframe. The matrix has to contain the                   #
# independent and all dependent variables of a linear regression model y=Xb+e .#
# Simply use "hansen(matrix)" to calculate the MMA estimator of b              #
# If y is not in the first column, specify it via "ycol=*".                    #
# The weights are suppressed if you choose "show.weights=FALSE"                #
################################################################################

# you need the following libraries to run "hansen()"      : quadprog
# you need the following libraries to run the illustration: ---

##################
## Illustration ##
##################

# Consider some arbitrary simulated data
x1 <- runif(1000,20,30)
x2 <- rpois(1000,7)
x3 <- rbinom(1000,100,0.4)
eps <- rnorm(1000,0,4)
eps2 <- rnorm(1000)
x4 <- (4*(x2)^2 + 8 +eps)/100
x5 <- 100-2*x1+eps2
x6 <- rbinom(1000,4,0.6)
eps3 <- rnorm(1000,0,4)

mu <- -10 + 4*x3 + 0.1*x4 + eps3
y <- rnorm(1000,mu,3)  

# Create a MATRIX, NOT A DATAFRAME
dataexample <- cbind(y,x1,x2,x3,x4,x5,as.factor(x6))

# Evaluate the data due to Hansen
# Consider the linear regression model y=a + b1x1 + ... + b6x6 + e and all nested submodels
# With "ycol" you specify the column in which is your response 
# Note that ALL other Variables that are in the dataset are treated as covariates
# The function returns the weights, and the (model-)averaged estimate

hansen(dataexample)
hansen(dataexample,ycol=1,show.weights=FALSE)
