You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言Fama-French模型回归时Newey-West检验遇参数长度为零问题

Fixing the "NeweyWest - argument of length zero" Error in Your Crypto Fama-French Model

Hey there! As someone who’s worked through plenty of R regression hiccups (especially with asset pricing models), let’s break down why you’re hitting this error and how to fix it.

First, let’s recap the core issue: your lm() regression runs fine, but NeweyWest() throws an "argument of length zero" message. Here are the most likely fixes, ordered by how common they are for new R users:

1. You’re Passing Vectors Instead of an lm Model Object

The #1 mistake here is trying to feed raw vectors (like r, CRIX, etc.) directly into NeweyWest(). This function expects a fitted linear model object (the output from lm()), not individual variables.

Correct Approach:

# First, fit your model properly and store the result
ff_model <- lm(r ~ CRIX + SMB + HML)

# Don't forget to load the sandwich package first!
library(sandwich)
# Pass the model object to NeweyWest()
nw_results <- NeweyWest(ff_model)

# View the adjusted standard errors
nw_results

2. Missing Values Are Messing Up Your Model Fit

If any of your vectors (r, CRIX, SMB, HML) have NA values, lm() will automatically drop those rows. In extreme cases, this could leave you with zero valid observations (though your lm() would have warned you about this). Let’s check and clean your data:

# Combine all variables into a data frame for easier management
crypto_data <- data.frame(r = r, CRIX = CRIX, SMB = SMB, HML = HML)

# Check how many rows have missing values
cat("Number of incomplete rows:", sum(!complete.cases(crypto_data)), "\n")

# Clean the data by removing rows with NAs
clean_data <- crypto_data[complete.cases(crypto_data), ]

# Refit the model with clean data
ff_model_clean <- lm(r ~ CRIX + SMB + HML, data = clean_data)

# Run Newey-West again
nw_results_clean <- NeweyWest(ff_model_clean)

3. Variable Environment or Naming Issues

If you used attach() to load your variables, or if there’s a typo in a variable name, NeweyWest() might not be able to find the underlying data tied to your model. Using a data frame with the data argument in lm() avoids this entirely (as shown in the code above).

4. Verify the Sandwich Package is Installed and Loaded

NeweyWest() is part of the sandwich package—if you haven’t installed it yet, run:

install.packages("sandwich")

Then load it with library(sandwich) before calling the function. Forgetting this can sometimes lead to weird error messages, though "argument of length zero" is usually not the main one here.

Full Working Example (With Simulated Data)

To test if this works, try running this code with simulated data that matches your setup:

# Load required package
library(sandwich)

# Simulate 364 observations matching your data length
set.seed(42)  # For reproducibility
r <- rnorm(364)
CRIX <- rnorm(364)
SMB <- rnorm(364)
HML <- rnorm(364)

# Fit model
ff_model <- lm(r ~ CRIX + SMB + HML)

# Run Newey-West
NeweyWest(ff_model)

This should output the Newey-West adjusted standard errors without any errors.

内容的提问来源于stack exchange,提问作者Henning Wode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:20:08