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

如何在R语言中格式化含国家与年份的数据以进行回归分析?

Got it, let's walk through how to run linear regressions with one explanatory variable against multiple time-series dependent variables in R. Here's a practical, step-by-step guide tailored to your needs:

1. Data Preparation

First, let's make sure your data is structured correctly. Since you're working with time series, you can either use a data.frame (with a time column) or keep variables as ts objects. Below is an example of simulating data that matches your description—replace this with your actual dataset:

# Load tidyverse for data manipulation (optional but helpful)
library(tidyverse)

# Simulate time-series data (replace with your real data)
set.seed(123) # For reproducible results
time_periods <- seq(as.Date("2010-01-01"), as.Date("2023-12-01"), by = "month")

# Explanatory variable
explanatory_index <- ts(
  rnorm(length(time_periods), mean = 100, sd = 10),
  start = c(2010, 1), frequency = 12
)

# Dependent variables (GDP, market_size, plus an extra example: sales)
GDP <- ts(
  50 + 2*explanatory_index + rnorm(length(time_periods), sd = 5),
  start = c(2010, 1), frequency = 12
)
market_size <- ts(
  100 + 1.5*explanatory_index + rnorm(length(time_periods), sd = 8),
  start = c(2010, 1), frequency = 12
)
sales <- ts(
  20 + 0.8*explanatory_index + rnorm(length(time_periods), sd = 3),
  start = c(2010, 1), frequency = 12
)

# Combine into a data frame (easier for regression workflows)
df <- data.frame(
  time = time_periods,
  explanatory_index = as.numeric(explanatory_index),
  GDP = as.numeric(GDP),
  market_size = as.numeric(market_size),
  sales = as.numeric(sales)
)

2. Option 1: Run Regressions One-by-One

If you want to examine each model in detail (e.g., check residuals, coefficients), run individual regressions using lm():

# Regression for GDP
model_gdp <- lm(GDP ~ explanatory_index, data = df)
summary(model_gdp) # View full stats, coefficients, p-values

# Regression for market_size
model_market <- lm(market_size ~ explanatory_index, data = df)
summary(model_market)

# Regression for sales
model_sales <- lm(sales ~ explanatory_index, data = df)
summary(model_sales)

3. Option 2: Batch Process All Dependent Variables

For efficiency (especially if you have many dependent variables), use purrr to run all regressions at once and compile results:

# Define the list of dependent variable names
dep_vars <- c("GDP", "market_size", "sales")

# Run all regressions in a loop
models <- map(
  dep_vars,
  ~lm(reformulate("explanatory_index", response = .x), data = df)
)

# Name the models for clarity
names(models) <- dep_vars

# View summaries for all models
map(models, summary)

# Extract key metrics into a clean data frame (coefficients, p-values, R²)
results_summary <- map_dfr(models, function(model) {
  stats <- summary(model)
  tibble(
    coefficient = stats$coefficients[2, 1],
    p_value = stats$coefficients[2, 4],
    r_squared = stats$r.squared
  )
}, .id = "dependent_variable")

print(results_summary)

4. Critical Time-Series Considerations

Since your data is time series, don't forget to check for autocorrelation in residuals—a common issue that can bias standard errors:

library(forecast)
# Check residuals for the GDP model (repeat for others)
checkresiduals(model_gdp)

# If autocorrelation exists, use Newey-West standard errors to correct it
library(sandwich)
library(lmtest)
coeftest(model_gdp, vcov = NeweyWest(model_gdp))

This adjusts for autocorrelation and heteroscedasticity, giving you more reliable p-values.

Feel free to tweak the code to match your actual data structure (e.g., if you're working directly with ts objects instead of a data frame). If you need help interpreting specific outputs or troubleshooting issues, let me know!

内容的提问来源于stack exchange,提问作者John Doe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:00:25