Loop lm and msmFit extracting coefficients


#1

Dear all,

I have an xts object with 81 variables. Of these, I need to extract 25 of them with a common string. For each element of this subset I need to do the following estimation, which works for one element (PortAvilliq#). Firstly, a lm, then a msmFit.

Port1 <- lm(PortAvilliq1 ~ 1, data = ger_ts)
# The msmFit
summary(msmPort1 <- msmFit(Port1, 2, sw=rep(TRUE,2)))
# Two variables to determine the greater and smaller coefficients
Port1HighIll <- ifelse(msmPort1@Coef[1,]> msmPort1@Coef[2,],msmPort1@Coef[1,], 
msmPort1@Coef[2,])
Port1LowIll <- ifelse(msmPort1@Coef[1,] < msmPort1@Coef[2,], msmPort1@Coef[1,],
msmPort1@Coef[2,])
# The associated probabilities
Port1ProbLow <- ifelse(msmPort1@Coef[1,] > msmPort1@Coef[2,], msmPort1@transMat[2,2],
msmPort1@transMat[1,1])
Port1ProbHigh <- ifelse(msmPort1@Coef[1,] > msmPort1@Coef[2,], msmPort1@transMat[1,1],
msmPort1@transMat[2,2])
# The main variable of interest 
Port1EIll <- Port1LowIll * Port1ProbLow + (Port1HighIll-Port1LowIll) * Port1ProbHigh`

I could workout

myNames <- paste("PortAvilliq", 1:25, sep = "")
myNames <- subset(datager, select = myNames)
mycoefs <- lapply(myNames, function(myNames) mod <-  lm(myNames ~ 1))

However, now I'm stuck as to run the msmFit for each model and then extract coefficients in an array

How can I do it?

Thanks for any help


#2

I think you're pretty close. If you create a function for doing the estimation, then you're just a few steps from getting your result.

I'm assuming you're using the package MSwM. Also, I don't have access to your data, so I'm using the energy data set included in MSwM.

library(MSwM)
data("energy")

I removed the prefix Port1 and the suffix Ill from your code and created a function that takes a model and extracts the relevant values as a named vector.

get_coeff <- function(msm) {
  # Two variables to determine the greater and smaller coefficients
  High <- ifelse(msm@Coef[1,]> msm@Coef[2,],msm@Coef[1,], 
                         msm@Coef[2,])
  Low <- ifelse(msm@Coef[1,] < msm@Coef[2,], msm@Coef[1,],
                        msm@Coef[2,])
  # The associated probabilities
  ProbLow <- ifelse(msm@Coef[1,] > msm@Coef[2,], msm@transMat[2,2],
                         msm@transMat[1,1])
  ProbHigh <- ifelse(msm@Coef[1,] > msm@Coef[2,], msm@transMat[1,1],
                          msm@transMat[2,2])
  # The main variable of interest 
  E <- Low * ProbLow + (High-Low) * ProbHigh
  c(High = High, 
    Low = Low,
    ProbHigh = ProbHigh,
    ProbLow = ProbLow,
    E = E
    )
}

In the following section, I create a vector of variable names, which I convert them into a list of formulas, which I then I use to create a list of linear models. From this, I use lapply to create a list of msm models, from which I apply the function above, which gives me a list coefficient vectors.

vars <- c("Oil", "Gas", "Coal")
forms <- lapply(vars, function(v) as.formula(paste(v, '~ 1')))
lm_mods <- lapply(forms, function(f) lm(f, data = energy))
ms_mods <- lapply(lm_mods, function(m) msmFit(m, 2, sw = rep(TRUE,2)))
ms_coeff <- lapply(ms_mods, get_coeff)

The last step is to give each vector a name. This allows us to create a dataframe, which can be converted to a matrix.

names(ms_coeff) <- vars
as.matrix(as.data.frame(ms_coeff))