1

Specifically, I am trying to create a function that can pull BIC values from a bunch of lm models on time series data. The time series has a lagged effects which are formatted using embed,example shown here:

library(TMS)
dts_matrix <- apply(ffrate_tsm,2,diff)
mixed_dts_matrix <- cbind(diff(ffrate_tsm[,1:3]),ffrate_tsm[2:218,4:5])
FFR <- embed(diff(ffr),4)
INF <- embed(infl[2:218],4)
##variables removed to simplify example

I wanted to see if there was a way to collect a vector of BIC so I can compare them, essentially I needed this function done multiple times with different inputs, where the variable x ranges from 1 to 4. The result is a single value, that needs to be stored multiple times as well.

BIC(lm(FFR[,1] ~ FFR[,2:x] + 
                     GAP[,1:x] + 
                     INF[,1:x] + 
                     INFX[,1:x] + 
                     SPREAD[,1:x]
))

I have tried creating a vector of x to repeat the code, and had an error message that looks like this: " numerical expression has 144 elements: only the first usednumerical expression has 144 elements: only the first usednumerical expression has 24 elements: only the first usednumerical expression has 6 elements: only the first usednumerical expression has 2 elements: only the first used "

Here was the final code I used to create such a matrix for the repeated function:

fourlag <- matrix(0,nrow=144,ncol=6)
colnames(fourlag) <- c("FFR","GAP","INF","INFX","SPREAD","BIC")
fourlag[1,] <- c(3,3,4,3,2,0)
fourlag[,5] <- rep(1:2)
fourlag[,4] <- rep(1:3,each=2)
fourlag[,3] <- rep(1:4,each=6)
fourlag[,2] <- rep(1:3,each=24)
fourlag[,1] <- rep(2:3,each=72)

fourlag[,6] <- BIC(lm(FFR[,1] ~ 
                        FFR[,2:fourlag[,1]] + 
                     GAP[,1:fourlag[,2]] + 
                     INF[,1:fourlag[,3]] + 
                     INFX[,1:fourlag[,4]] + 
                     SPREAD[,1:fourlag[,5]]
                     ))

Reproducible example:

dput(head(ffrate_tsm))
structure(c(9.2, 8.95, 8.68, 8.51, 8.77, 8.8, -0.15677, -0.03707, 
0.1142, 0.29597, 0.48071, 0.56044, -0.26918, -0.26184, -0.23337, 
-0.19923, -0.16417, -0.13182, -0.151, -0.261, -0.3706, -0.3694, 
-0.23, -0.0016, -1.22, -0.72, 0.12, 0, 0.01, -0.45), .Dim = 6:5, .Dimnames = list(
    NULL, c("ffr", "gap", "infl", "infex", "spread")), .Tsp = c(1982.83333333333, 
1983.25, 12), class = c("mts", "ts", "matrix"))
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
W Hoelt
  • 11
  • 2
  • You can use a `for` loop to apply a function to each column of a matrix or data.frame. If you need additional assistance please provide a reproducible example by clicking [edit] on your question and paste the the output of `dput(ffrate_tsm)` or `dput(head(ffrate_tsm))` if your data is very large. See [How to make a great R reproducible example](https://stackoverflow.com/a/5963610/) for more. – Ian Campbell Apr 18 '21 at 18:50
  • Hey Ian, thanks for some feedback. II pasted the output of the `dput(head(ffrate_tsm))` at the bottom. I believe I am misusing the `FUN` input for apply. I get a similar error message of, with inclusions of my `FUN` is not a function. Here is how I wrote that function: `fourlag[,6] <- apply(X=fourlag,MARGIN=2,FUN=BIC(lm(FFR[,1] ~ FFR[,2:fourlag[,1]] + GAP[,1:fourlag[,2]] + INF[,1:fourlag[,3]] )))` – W Hoelt Apr 18 '21 at 19:00

0 Answers0