jacob-long / jacob-long/jtools

About sim_slopes function to conduct clustered robust

Open
#163 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
R
Stars
171
Forks
23
PR merge metrics
No merged PRs in 30d

Description

Hello, help! I'm wondering how to use the sim_slopes function to conduct clustered robust plotting based on the `gvkey `of enterprises. I've tried using `cluster`, `v.cov`, and `v.cov.args`, but none of them worked:

stata code: reg y c.x##c.mod i.fyear i.industry, cluster(gvkey)

```
fit1 <- lm(y ~ x * mod + factor(industry) + factor(fyear), data = data)
cov_matrix <- vcovCL(fit1, cluster = data$gvkey)
cov_matrix2 = coeftest(fit1, vcov = cov_matrix)

sim_slopes(fit1, pred = x, modx = mod, jnplot = TRUE, cluster = "gvkey", data = merged_data)
# There was no error message, but the plotting result was the same as when not adding cluster = "gvkey". I guess it didn't take effect.

sim_slopes(fit1, pred = x, modx = mod, jnplot = TRUE, v.cov = sandwich::vcovCL(fit1, cluster = data$gvkey), v.cov.args = fit1)
# Error information: vcovargs[[which(sapply(vcovargs, function(x) length(x[[1]]) == : attempt to select less than one element in OneIndex

sim_slopes(fit1,
pred = x,
modx = mod,
jnplot = TRUE,
v.cov = sandwich::vcovCL(fit1, cluster = data$gvkey), # Specify the function for dynamically calculating the covariance
v.cov.args = fit1) # Specify the parameters for vcovCL
# Error information: vcovargs[[which(sapply(vcovargs, function(x) length(x[[1]]) == : attempt to select less than one element in OneIndex
```

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the sim_slopes entry point and trace how cluster, v.cov, and v.cov.args are parsed. Reproduce the supplied lm and sandwich::vcovCL examples, then verify that clustered covariance changes the plotted results and that the v.cov.args form no longer errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
analytics
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.