使用R语言eventstudies包复现eventstudy函数时遇阻求助
eventstudies Package Issue in R Let’s walk through how to get your event study code working. I’ve tackled similar problems with this package before, so here are the key areas to check:
1. Verify Your Data Formats Match Package Requirements
The eventstudy() function is picky about input structures—let’s confirm your data fits:
- StockPriceReturns (firm.returns): This should be either an
xtsobject or a data frame where:- Rows represent dates (must be a standard
Datetype, not character strings) - Columns are individual stocks, with column names matching the
firm.identifiervalues in your SplitDates data - All values are numeric (no character-based returns or missing values). Run
str(StockPriceReturns)andanyNA(StockPriceReturns)to validate.
- Rows represent dates (must be a standard
- SplitDates (event.list): This needs to be a data frame with at least two columns:
firm.identifier: Exact matches to the column names in StockPriceReturns (e.g., stock tickers)event.date: ADate-type column with the date of each stock’s split event- Double-check that each event date falls within the date range of your StockPriceReturns data—events outside this range will cause failures.
2. Double-Check Function Parameters
Let’s break down the parameters you’re using to make sure they align with your data:
event.window = 7: This sets a total window of 7 days (3 days before the event, the event day, 3 days after). Ensure your StockPriceReturns has data for every day in this window for each stock in SplitDates—missing dates here will break the calculation.to.remap = TRUE+remap = "cumsum": This converts single-period returns to cumulative returns. Make sure your raw returns are single-period (not already cumulative)—using pre-cumulated data here will give nonsensical results.inference = TRUE+inference.strategy = "bootstrap": Bootstrap inference requires a sufficient sample size (enough stocks/events) and complete data. If you’re working with a small dataset, try running the code first withinference = FALSEto rule out issues with the bootstrap step.
3. Test with the Package’s Built-In Sample Data
First, confirm the package itself works by running the exact example code with the provided sample data:
library(eventstudies) data(StockPriceReturns) data(SplitDates) # Run the sample event study es <- eventstudy(firm.returns = StockPriceReturns, event.list = SplitDates, event.window = 7, type = "None", to.remap = TRUE, remap = "cumsum", inference = TRUE, inference.strategy = "bootstrap") # Check the results summary(es)
If this runs successfully, the problem is definitely with your custom data. If it fails, try reinstalling the package with dependencies:
install.packages("eventstudies", dependencies = TRUE)
4. Use Error Messages to Narrow Down Issues
When you run your code with your own data, R will output specific error messages (e.g., "invalid date format", "firm identifier not found"). These messages are your best clue—if you hit a specific error, cross-reference it with the checks above (e.g., a date error means your event dates or return dates aren’t properly formatted as Date types).
内容的提问来源于stack exchange,提问作者Niall Skelton

