Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to fix "variable lengths differ" problem in linear regression model?

    Hi, I don't understand why the following codes generate error message:

    Code:
    library(wooldridge)
    library(jtools)
    library(huxtable)
    data('card')
    
    
    fit1 <- lm(card$educ~card$nearc4)
    fit2 <- lm(card$educ~card$nearc4 + card$motheduc + card$fatheduc)
    fit3 <- lm(card$lwage~card$nearc4)
    
    
    if (requireNamespace("huxtable")) {
      # Export all 3 regressions with "Model #" labels,
      # standardized coefficients, and robust standard errors
      export_summs(fit1, fit2, fit3,
                   model.names = c("Model 1","Model 2","Model 3"),
                   scale = TRUE, robust = TRUE)
    }
    And the error is:

    Code:
    Error in model.frame.default(formula = card$educ ~ `card$nearc4` + `card$motheduc` +  :   variable lengths differ (found for 'card$nearc4')
    I checked the length of each variable, which is all the same. Each fitted model works without any issue, but `export_summs()` seems to generate a problem. Is there any mistake in my code?

    Thanks for help.
    Last edited by Chul-Kyoo Jung; 04 Apr 2021, 15:06.

  • #2
    this is Statalist and deals with the Stata program; your code is for R and thus this is the wrong place to ask a question about R; please go to an R list

    Comment


    • #3
      I'm new here so I am not sure if R questions are accepted (this is a Stata forum). I'll answer it anyway and beware that this thread may be deleted.

      That error is often caused by missing data. If you check the summary:

      Code:
      summary(fit1)
      summary(fit2)
      summary(fit3)
      You'll see that model 2 has about 800 fewer cases in it. Along the same vein, if you remove "fit2" and "Model 2" from the huxtable code chunk, you'll find it working.

      Comment


      • #4
        So sorry. Apology for my mistake. I was totally confused with Stata and r. I will remove this post right away. Thanks for all responses!
        Last edited by Chul-Kyoo Jung; 04 Apr 2021, 15:47.

        Comment

        Working...
        X