在R中生成iid二元变量X=(x1,x2)的技术求助
Got it, let's break down how to create this bivariate variable and plot it in R step by step. First, let's align on the requirements: we need 1000 independent, identically distributed (iid) observations where x1 follows a standard normal distribution, and x2 is approximately equal to x1 when x1 is between -1 and 1, exactly equal to x1 otherwise.
Step 1: Generate the Standard Normal x1
First, we'll create x1 using R's built-in normal distribution function. Setting a random seed ensures your results are reproducible (you can change the seed value or remove this line if you don't need consistency).
# Set seed for reproducible results set.seed(123) # Generate 1000 standard normal observations for x1 sample_size <- 1000 x1 <- rnorm(sample_size, mean = 0, sd = 1)
Step 2: Construct x2 Based on x1
You mentioned x2 is "approximately equal to x1" when -1 ≤ x1 ≤ 1. A common way to implement this is adding a small amount of random noise to x1 in that interval (you can adjust the noise level to match your definition of "approximate"). For values outside [-1, 1], we just use x1 directly.
# Create x2: add small normal noise to x1 in [-1,1], use x1 otherwise x2 <- ifelse(abs(x1) <= 1, x1 + rnorm(sample_size, mean = 0, sd = 0.1), # Small noise for "approximate" x1)
Customization Tip:
If your "approximate" means something else (e.g., uniform noise instead of normal, or fixed rounding), tweak the noise line:
- For uniform noise between -0.1 and 0.1:
x1 + runif(sample_size, -0.1, 0.1) - For exact equality (if "approximate" was a typo): just use
x1instead of the noise line
Step 3: Visualize the Bivariate Data
We'll cover two common plotting approaches: base R (quick and simple) and ggplot2 (customizable and polished).
Option 1: Base R Scatter Plot
Great for fast exploration:
# Scatter plot with transparency to avoid overlapping points plot(x1, x2, pch = 16, # Solid circle points col = adjustcolor("darkblue", alpha.f = 0.5), # Semi-transparent blue main = "Scatter Plot of X=(x1, x2)", xlab = "x1 ~ N(0,1)", ylab = "x2") # Add dashed red lines to mark the [-1, 1] interval for x1 abline(v = c(-1, 1), lty = 2, col = "red")
Option 2: ggplot2 (Tidyverse Style)
If you prefer more control over aesthetics, use ggplot2 (install it first with install.packages("ggplot2") if you haven't):
library(ggplot2) # Convert data to a data frame (required for ggplot) bivariate_data <- data.frame(x1 = x1, x2 = x2) ggplot(bivariate_data, aes(x = x1, y = x2)) + geom_point(alpha = 0.5, color = "darkblue") + # Semi-transparent points geom_vline(xintercept = c(-1, 1), linetype = "dashed", color = "red") + # Interval markers labs(title = "Scatter Plot of Bivariate Variable X=(x1, x2)", x = "x1 ~ N(0,1)", y = "x2") + theme_minimal()
Quick Check
To verify your data matches expectations, run these lines to summarize key stats:
# Summary stats for x1 and x2 summary(x1) summary(x2) # Check how many x1 values fall in [-1,1] sum(abs(x1) <= 1) / sample_size # Should be ~68% (empirical rule for normal distribution)
内容的提问来源于stack exchange,提问作者Derek

