【发布时间】:2014-10-29 23:50:20
【问题描述】:
我正在尝试为一群儿童模拟两个体重和年龄值。这些数据应该是 S 型相关的,这样在低年龄时体重变化缓慢,然后到月经后大约 30 周时体重增加会加速,大约 50 周后开始趋于平稳。
我已经能够使用下面的代码来获得体重和年龄之间的线性相关性,以便很好地工作。我遇到问题的部分是调整此代码以使数据具有更 S 形的形状。任何建议将不胜感激。
# Load required packages
library(MASS)
library(ggplot2)
# Set the number of simulated data points
n <- 100
# Set the mean and standard deviations for
# the two variables
mean_age <- 50
sd_age <- 20
mean_wt <- 10
sd_wt <- 4
# Set the desired level of correlation
# between the two variables
cor_agewt <- 0.9
# Build the covariance matrix
covmat <- matrix(c(sd_age^2, cor_agewt * sd_age * sd_wt,
cor_agewt * sd_age * sd_wt, sd_wt^2),
nrow = 2, ncol = 2, byrow = TRUE)
# Simulate the correlated results
res <- mvrnorm(n, c(mean_age, mean_wt), covmat)
# Reorganize the simulate data into a data frame
df <- data.frame(age = res[,1],
wt = res[,2])
# Plot the results and fit a loess spline
# to the data
ggplot(df, aes(x = age, y = wt)) +
geom_point() +
stat_smooth(method = 'loess')
电流输出:
理想的输出(尽管年龄和体重范围较小):
【问题讨论】:
标签: r variables simulation correlation