1

I'm attempting to write a code that runs multiple regression (OLS, FE, RE) models on some financial firm data, but am encountering an error when defining new variables. Using the below code I'm trying to define new lag/lead variables using variables I have already defined in previous code:

## Lead/Lag variables
library(dplyr)
df <- df[with(df,order(gvkey,fyear)), ]

df <- df %>%
  group_by(gvkey) %>%
  mutate(lagfyear = lag(fyear,1),
         lagat = lag(at,1),
         lagcash = lag(cash,1),
         lagtang = lag(Q,1),
         lagzscore = lag(zscore,1),
         lagQ = lag(Q,1),
         leadQ = lead(Q,1),
         leadroa = lead(roa, 1),
         leadz = lead(zscore,1),
         leadrd = lead(rd_lagat,1),
         leadlogmktcap = lead(tang,1),
         leadtang = lead(tang, 1),
         leadcf = lead(cf, 1),
         leadlogsale = lead(logsale,1)) %>%
  as.data.frame()

I'm getting the following error:

Error: Problem with mutate() column leadQ. i leadQ = lead(Q, 1). x no applicable method for 'lead' applied to an object of class "c('double', 'numeric')" i The error occurred in group 1: gvkey = 1004.

gvkey is the unique key assigned to each firm. Q is another variable that's been defined as market value/total replacement value of the firm.

This code works for my colleagues, so I'm not sure why it's not working for me. Any help is appreciated.

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
zwt_
  • 11
  • 2
  • 2
    Please provide a complete example including all library statements and input in reproducible form so that others can reproduce the problem. See the top of the [tag:r] tag page for info on asking questions. – G. Grothendieck May 28 '21 at 19:32

1 Answers1

0

I think the answer is written down here.

In short, I think you are using the wrong lag function. Try using dplyr::lag instead.

Jinhua Wang
  • 1,679
  • 1
  • 17
  • 44