Monetary Policy and Federal Funds Rate: Applying a GLM--Logistic Regression to Federal funds and CPI data

Perhaps LR is not best, LM is better, or a different GLM. I am not sure as of yet. I know I can wrap this up today, but I do need some guidance to finish strong.

I might be analyzing .csv files backward. I want to train via FEDERALFUNDS.csv for the rate by date for the testing file the fredgraph via the cpi variables.

Here is my progress and error message below:

table(FEDERALFUNDS$Good.Date)
< table of extent 0 >

table(FEDERALFUNDS)
FEDFUNDS
DATE 0.05 0.06 0.07 0.08 0.09 0.1 0.2 0.33 0.65 1.55 1.58 1.83 2.04 2.13 2.38 2.39 2.4 2.42
1/1/2020 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
1/1/2021 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1/1/2022 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
10/1/2019 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
10/1/2020 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
10/1/2021 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
11/1/2019 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
11/1/2020 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
11/1/2021 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
12/1/2019 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
12/1/2020 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
12/1/2021 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2/1/2020 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
2/1/2021 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2/1/2022 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3/1/2020 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
3/1/2021 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3/1/2022 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
4/1/2019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
4/1/2020 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4/1/2021 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4/1/2022 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
5/1/2019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
5/1/2020 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5/1/2021 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6/1/2019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
6/1/2020 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
6/1/2021 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7/1/2019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
7/1/2020 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
7/1/2021 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
8/1/2019 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
8/1/2020 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
8/1/2021 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
9/1/2019 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
9/1/2020 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
9/1/2021 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
fitfull <-glm(fredgraph$gooddate ~ CPIAUCSL_PC1 +

  •           JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1, family=binomial())
    

Error in eval(predvars, data, env) : object 'CPIAUCSL_PC1' not found

attach(fredgraph)
The following object is masked from FEDERALFUNDS:

DATE

attach(fredgraph)
The following objects are masked from fredgraph (pos = 3):

CP0000EZ19M086NEST_PC1, CPIAUCSL_PC1, DATE, GBRCPIALLMINMEI_PC1, JPNCPIALLMINMEI_PC1

The following object is masked from FEDERALFUNDS:

DATE

R Output
Below is all of the code I used whether successfully unsuccessfully as well as all the outputs, error messages, explanations, and my questions.

summary(fredgraph)
DATE CPIAUCSL_PC1
Length:37 Min. :0.2424
Class :character 1st Qu.:1.4006
Mode :character Median :2.0056
Mean :3.2502
3rd Qu.:5.2761
Max. :8.5576
JPNCPIALLMINMEI_PC1 GBRCPIALLMINMEI_PC1
Length:37 Length:37
Class :character Class :character
Mode :character Mode :character

str(fredgraph)
'data.frame': 37 obs. of 5 variables:
DATE : chr "4/1/2019" "5/1/2019" "6/1/2019" "7/1/2019" ... CPIAUCSL_PC1 : num 2.01 1.81 1.69 1.82 1.76 ...
JPNCPIALLMINMEI_PC1 : chr "0.90817" "0.70493" "0.60484" "0.60484" ... GBRCPIALLMINMEI_PC1 : chr "1.99052" "1.88857" "1.88857" "1.983" ...
$ CP0000EZ19M086NEST_PC1: num 1.72 1.22 1.27 1.02 1.01 ...
CP0000EZ19M086NEST_PC1
Min. :-0.3134
1st Qu.: 0.7260
Median : 1.2215
Mean : 1.8940
3rd Qu.: 2.1648
Max. : 7.4439

names(fredgraph)
[1] "DATE" "CPIAUCSL_PC1"
[3] "JPNCPIALLMINMEI_PC1" "GBRCPIALLMINMEI_PC1"
[5] "CP0000EZ19M086NEST_PC1"

FEDERALFUNDS <-read.csv("FEDERALFUNDS.csv")

summary(FEDERALFUNDS)
DATE FEDFUNDS
Length:37 Min. :0.0500
Class :character 1st Qu.:0.0800
Mode :character Median :0.0900
Mean :0.6716
3rd Qu.:1.5500
Max. :2.4200
str(FEDERALFUNDS)
'data.frame': 37 obs. of 2 variables:
DATE : chr "4/1/2019" "5/1/2019" "6/1/2019" "7/1/2019" ... FEDFUNDS: num 2.42 2.39 2.38 2.4 2.13 2.04 1.83 1.55 1.55 1.55 ...
names(FEDERALFUNDS)
[1] "DATE" "FEDFUNDS"

Raw data for Fed Funds rate:

The variables analyzed above are the federal funds rate and the CPI rates during a specific month and year.

FEDERALFUNDS <-read.csv("FEDERALFUNDS.csv")

FEDERALFUNDS
DATE FEDFUNDS
1 4/1/2019 2.42
2 5/1/2019 2.39
3 6/1/2019 2.38
4 7/1/2019 2.40
5 8/1/2019 2.13
6 9/1/2019 2.04
7 10/1/2019 1.83
8 11/1/2019 1.55
9 12/1/2019 1.55
10 1/1/2020 1.55
11 2/1/2020 1.58
12 3/1/2020 0.65
13 4/1/2020 0.05
14 5/1/2020 0.05
15 6/1/2020 0.08
16 7/1/2020 0.09
17 8/1/2020 0.10
18 9/1/2020 0.09
19 10/1/2020 0.09
20 11/1/2020 0.09
21 12/1/2020 0.09
22 1/1/2021 0.09
23 2/1/2021 0.08
24 3/1/2021 0.07
25 4/1/2021 0.07
26 5/1/2021 0.06
27 6/1/2021 0.08
28 7/1/2021 0.10
29 8/1/2021 0.09
30 9/1/2021 0.08
31 10/1/2021 0.08
32 11/1/2021 0.08
33 12/1/2021 0.08
34 1/1/2022 0.08
35 2/1/2022 0.08
36 3/1/2022 0.20
37 4/1/2022 0.33
DATE FEDFUNDS
Length:37 Min. :0.0500
Class :character 1st Qu.:0.0800
Mode :character Median :0.0900
Mean :0.6716
3rd Qu.:1.5500
Max. :2.4200
DATE CPIAUCSL_PC1 JPNCPIALLMINMEI_PC1 GBRCPIALLMINMEI_PC1 CP0000EZ19M086NEST_PC1
1 4/1/2019 2.00563 0.90817 1.99052 1.72280
2 5/1/2019 1.81275 0.70493 1.88857 1.22151
3 6/1/2019 1.68964 0.60484 1.88857 1.26813
4 7/1/2019 1.82116 0.60484 1.983 1.02070
5 8/1/2019 1.75969 0.2004 1.69014 1.00952
6 9/1/2019 1.72733 0.2002 1.68856 0.83317
7 10/1/2019 1.75708 0.1996 1.49953 0.72602
8 11/1/2019 2.03082 0.5 1.49673 0.96061
9 12/1/2019 2.26172 0.80241 1.30719 1.32629
10 1/1/2020 2.46049 0.80241 1.78571 1.35962
11 2/1/2020 2.31971 0.60181 1.68539 1.21975
12 3/1/2020 1.53064 0.60181 1.49533 0.74748
13 4/1/2020 0.36486 0.2 0.92937 0.31399
14 5/1/2020 0.24244 0.1 0.64875 0.08552
15 6/1/2020 0.72681 0.1002 0.83411 0.26563
16 7/1/2020 1.03283 0.2004 1.11111 0.39081
17 8/1/2020 1.32759 0.1 0.46168 -0.17133
18 9/1/2020 1.40065 -0.1998 0.73801 -0.31342
19 10/1/2020 1.18420 -0.59761 0.83102 -0.27504
20 11/1/2020 1.13776 -0.99502 0.553 -0.28544
21 12/1/2020 1.27815 -1.19403 0.82949 -0.26558
22 1/1/2021 1.35997 -0.69652 0.92336 0.91022
23 2/1/2021 1.67524 -0.4985 0.73665 0.93726
24 3/1/2021 2.65838 -0.3988 1.01289 1.33168
25 4/1/2021 4.15199 -1.0978 1.65746 1.62193
26 5/1/2021 4.94444 -0.6993 2.20994 1.98424
27 6/1/2021 5.34102 -0.4004 2.38971 1.90179
28 7/1/2021 5.27610 -0.3 2.01465 2.16483
29 8/1/2021 5.20533 -0.3996 3.03309 2.95576
30 9/1/2021 5.38991 0.2002 2.9304 3.36319
31 10/1/2021 6.23694 0.1002 3.84615 4.05136
32 11/1/2021 6.82837 0.60302 4.58295 4.86641
33 12/1/2021 7.09654 0.80564 4.84461 4.96434
34 1/1/2022 7.52593 0.501 4.84904 5.10824
35 2/1/2022 7.91202 0.9018 5.48446 5.87455
36 3/1/2022 8.55759 1.2012 6.19872 7.44391
37 4/1/2022 8.22414 . . 7.43887

I am able to organize the data, summarize and I decided to use linear regression but when I attempted to apply LM I get these issues:

MODEL1 <- lm(FEDERALFUNDS ~ DATE + CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1)
Error in eval(predvars, data, env) : object 'DATE' not found

MODEL1 <- lm(FEDERALFUNDS ~ CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1)
Error in eval(predvars, data, env) : object 'CPIAUCSL_PC1' not found
data <- read.csv("fredgraph.csv", sep = ",", dec = ",")
regmodel <-lm(FEDERALFUNDS ~ CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1)
Error in eval(predvars, data, env) : object 'CPIAUCSL_PC1' not found
regmodel <-lm(fredgraph.csv ~ CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1)
Error in eval(predvars, data, env) : object 'fredgraph.csv' not found
regmodel <-lm(FEDERALFUNDS ~ CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1)
Error in eval(predvars, data, env) : object 'CPIAUCSL_PC1' not found
regmodel <-lm(FEDERALFUNDS ~ date)
Error in model.frame.default(formula = FEDERALFUNDS ~ date, drop.unused.levels = TRUE) :
invalid type (list) for variable 'FEDERALFUNDS'
regmodel <-lm(FEDERALFUNDS ~ CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1)
Error in eval(predvars, data, env) : object 'CPIAUCSL_PC1' not found
data <- read.csv("FEDERALFUNDS.csv", sep = ",", dec = ",")
regmodel <-lm(FEDERALFUNDS ~ CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1, data = Data)

set.seed(1)

row.number <- sample(1:nrow(FEDERALFUNDS), 0.8*nrow(FEDERALFUNDS))
train = FEDERALFUNDS[row.number,]
test =fredgraph[-row.numbe,]
Error in [.data.frame(fredgraph, -row.numbe, ) :
object 'row.numbe' not found
test = fredgraph[row.number,]
dim(train)
[1] 29 2
dim(test)
[1] 29 5

You are basically making the same mistake as with your previous posts. The thing is that columns do not exist as independent objects in your working environment, they exist as columns from a specific data frame so you can't simply call them by name without specifying from which data frame they come from, and even if you define the data argument for the function (which is the name of the data frame from where the variables you are referencing in the formula argument, come from), you can't reference columns from different data frames in the same formula.

From your code, I infer you have imported into memory two data frames, one called FEDERALFUNDS which has the columns DATE and FEDFUNDS, and the other called fredgraphwith the columns DATE, CPIAUCSL_PC1, JPNCPIALLMINMEI_PC1, GBRCPIALLMINMEI_PC1 and CP0000EZ19M086NEST_PC1. Then the code to train your linear model would be something like this:

library(dplyr)

# First join the two data frames into a single one so all variables are on the 
# same data frame, in this example a data frame called 'join_data'
join_data <- FEDERALFUNDS %>% 
    inner_join(fredgraph, by = 'DATE')

# Train the model (notice that the independent variable name is 'FEDFUNDS' not
# FEDERALFUNDS as you wrote the code you posted)
MODEL1 <- lm(formula = FEDFUNDS ~ DATE + CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1,
             data = join_data)

Obviously, I can't guarantee this exact code is going to work for you since you are not providing sample data in a usable format for me to test the code, but I hope you get the general idea.

EDIT: I have made the extra effort of parsing the data from the unformatted text you have posted so I can provide a proper Reproducible Example (Reprex), I have also cleaned the data a little bit since some numerical variables have been read as character strings. This is the result.

library(dplyr)
library(lubridate)

# Sample data on a copy/paste friendly format, you can omit this part
# since you already have the data frames read into memory from csv files
FEDERALFUNDS <- data.frame(
  stringsAsFactors = FALSE,
              DATE = c("4/1/2019","5/1/2019",
                       "6/1/2019","7/1/2019","8/1/2019","9/1/2019","10/1/2019",
                       "11/1/2019","12/1/2019","1/1/2020","2/1/2020","3/1/2020",
                       "4/1/2020","5/1/2020","6/1/2020","7/1/2020",
                       "8/1/2020","9/1/2020","10/1/2020","11/1/2020","12/1/2020",
                       "1/1/2021","2/1/2021","3/1/2021","4/1/2021","5/1/2021",
                       "6/1/2021","7/1/2021","8/1/2021","9/1/2021",
                       "10/1/2021","11/1/2021","12/1/2021","1/1/2022","2/1/2022",
                       "3/1/2022","4/1/2022"),
          FEDFUNDS = c(2.42,2.39,2.38,2.4,2.13,
                       2.04,1.83,1.55,1.55,1.55,1.58,0.65,0.05,0.05,0.08,
                       0.09,0.1,0.09,0.09,0.09,0.09,0.09,0.08,0.07,
                       0.07,0.06,0.08,0.1,0.09,0.08,0.08,0.08,0.08,0.08,
                       0.08,0.2,0.33)
)

fredgraph <- data.frame(
        stringsAsFactors = FALSE,
                    DATE = c("4/1/2019",
                             "5/1/2019","6/1/2019","7/1/2019","8/1/2019","9/1/2019",
                             "10/1/2019","11/1/2019","12/1/2019","1/1/2020",
                             "2/1/2020","3/1/2020","4/1/2020","5/1/2020",
                             "6/1/2020","7/1/2020","8/1/2020","9/1/2020",
                             "10/1/2020","11/1/2020","12/1/2020","1/1/2021",
                             "2/1/2021","3/1/2021","4/1/2021","5/1/2021",
                             "6/1/2021","7/1/2021","8/1/2021","9/1/2021",
                             "10/1/2021","11/1/2021","12/1/2021","1/1/2022","2/1/2022",
                             "3/1/2022","4/1/2022"),
            CPIAUCSL_PC1 = c(2.00563,1.81275,
                             1.68964,1.82116,1.75969,1.72733,1.75708,2.03082,
                             2.26172,2.46049,2.31971,1.53064,0.36486,
                             0.24244,0.72681,1.03283,1.32759,1.40065,1.1842,
                             1.13776,1.27815,1.35997,1.67524,2.65838,4.15199,
                             4.94444,5.34102,5.2761,5.20533,5.38991,
                             6.23694,6.82837,7.09654,7.52593,7.91202,8.55759,
                             8.22414),
     JPNCPIALLMINMEI_PC1 = c("0.90817",
                             "0.70493","0.60484","0.60484","0.2004","0.2002",
                             "0.1996","0.5","0.80241","0.80241","0.60181",
                             "0.60181","0.2","0.1","0.1002","0.2004","0.1",
                             "-0.1998","-0.59761","-0.99502","-1.19403","-0.69652",
                             "-0.4985","-0.3988","-1.0978","-0.6993",
                             "-0.4004","-0.3","-0.3996","0.2002","0.1002",
                             "0.60302","0.80564","0.501","0.9018","1.2012","."),
     GBRCPIALLMINMEI_PC1 = c("1.99052",
                             "1.88857","1.88857","1.983","1.69014","1.68856",
                             "1.49953","1.49673","1.30719","1.78571","1.68539",
                             "1.49533","0.92937","0.64875","0.83411","1.11111",
                             "0.46168","0.73801","0.83102","0.553",
                             "0.82949","0.92336","0.73665","1.01289","1.65746",
                             "2.20994","2.38971","2.01465","3.03309","2.9304",
                             "3.84615","4.58295","4.84461","4.84904",
                             "5.48446","6.19872","."),
  CP0000EZ19M086NEST_PC1 = c(1.7228,1.22151,
                             1.26813,1.0207,1.00952,0.83317,0.72602,0.96061,
                             1.32629,1.35962,1.21975,0.74748,0.31399,
                             0.08552,0.26563,0.39081,-0.17133,-0.31342,-0.27504,
                             -0.28544,-0.26558,0.91022,0.93726,1.33168,
                             1.62193,1.98424,1.90179,2.16483,2.95576,3.36319,
                             4.05136,4.86641,4.96434,5.10824,5.87455,7.44391,
                             7.43887)
)

# Relevant code
join_data <- FEDERALFUNDS %>% 
    inner_join(fredgraph %>% mutate(across(-DATE, as.numeric)), by = 'DATE') %>% 
    mutate(DATE = mdy(DATE))

MODEL1 <- lm(formula = FEDFUNDS ~ DATE + CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1,
             data = join_data)
MODEL1
#> 
#> Call:
#> lm(formula = FEDFUNDS ~ DATE + CPIAUCSL_PC1 + JPNCPIALLMINMEI_PC1 + 
#>     GBRCPIALLMINMEI_PC1 + CP0000EZ19M086NEST_PC1, data = join_data)
#> 
#> Coefficients:
#>            (Intercept)                    DATE            CPIAUCSL_PC1  
#>              86.590231               -0.004694                0.228127  
#>    JPNCPIALLMINMEI_PC1     GBRCPIALLMINMEI_PC1  CP0000EZ19M086NEST_PC1  
#>              -0.361887               -0.026911                0.239609

Created on 2022-05-21 by the reprex package (v2.0.1)

Thanks for the help--it was very useful. I completed the course strongly, and because of my stats background and my continuing to improve in R, I have job offers for a new career. Of course, I have more to learn in R, but I am improving.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.