R:使用动态大小的数组构建线性模型的循环结构
R: Loop structure to use dynamically sized arrays to build linear models
在循环的每次迭代中,我想使用更多的历史数据来拟合线性模型,并查看例如提前一步预测与实际情况的比较。代码应该是不言自明的。问题似乎是 Dependent 和 Independent 在第一次迭代后大小固定(我想从 10 个数据点开始,如代码所示),而我希望它们动态调整大小。
output1 <- rep(0, 127)
output2 <- rep(0, 127)
ret <- function(x, y)
{
for (i in 1:127)
{
Dependent <- y[1:(9+i)]
Independent <- x[1:(9+i)]
fit <- lm(Dependent ~ Independent)
nextInput <- data.frame(Independent = x[(10+i)])
prediction <- predict(fit, nextInput, interval="prediction")
output1[i] <- prediction[2]
output2[i] <- prediction[3]
}
}
这是一个想法,如果我接近你的意图,请告诉我:
set.seed(42)
n <- 100
x <- rnorm(n)
head(x)
# [1] 1.3709584 -0.5646982 0.3631284 0.6328626 0.4042683 -0.1061245
y <- runif(n)
head(y)
# [1] 0.8851177 0.5171111 0.8519310 0.4427963 0.1578801 0.4423246
ret <- lapply(10:n, function(i) {
dep <- y[1:i]
indep <- x[1:i]
fit <- lm(dep ~ indep)
pred <-
if (i < n) {
predict(fit, data.frame(indep = x[i+1L]), interval = "prediction")
} else NULL
list(fit = fit, pred = pred)
})
请注意,我正在制作一个 models/predictions 的列表,而不是使用 for
循环。尽管不完全相同,this answer 很好地解释了为什么这可能是个好主意。
其中一次运行的模型和预测:
ret[[50]]
# $fit
# Call:
# lm(formula = dep ~ indep)
# Coefficients:
# (Intercept) indep
# 0.44522 0.02691
# $pred
# fit lwr upr
# 1 0.4528911 -0.1160787 1.021861
summary(ret[[50]]$fit)
# Call:
# lm(formula = dep ~ indep)
# Residuals:
# Min 1Q Median 3Q Max
# -0.42619 -0.22178 -0.00004 0.15550 0.53774
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.44522 0.03667 12.141 <2e-16 ***
# indep 0.02691 0.03186 0.845 0.402
# ---
# Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Residual standard error: 0.2816 on 57 degrees of freedom
# Multiple R-squared: 0.01236, Adjusted R-squared: -0.004966
# F-statistic: 0.7134 on 1 and 57 DF, p-value: 0.4018
在循环的每次迭代中,我想使用更多的历史数据来拟合线性模型,并查看例如提前一步预测与实际情况的比较。代码应该是不言自明的。问题似乎是 Dependent 和 Independent 在第一次迭代后大小固定(我想从 10 个数据点开始,如代码所示),而我希望它们动态调整大小。
output1 <- rep(0, 127)
output2 <- rep(0, 127)
ret <- function(x, y)
{
for (i in 1:127)
{
Dependent <- y[1:(9+i)]
Independent <- x[1:(9+i)]
fit <- lm(Dependent ~ Independent)
nextInput <- data.frame(Independent = x[(10+i)])
prediction <- predict(fit, nextInput, interval="prediction")
output1[i] <- prediction[2]
output2[i] <- prediction[3]
}
}
这是一个想法,如果我接近你的意图,请告诉我:
set.seed(42)
n <- 100
x <- rnorm(n)
head(x)
# [1] 1.3709584 -0.5646982 0.3631284 0.6328626 0.4042683 -0.1061245
y <- runif(n)
head(y)
# [1] 0.8851177 0.5171111 0.8519310 0.4427963 0.1578801 0.4423246
ret <- lapply(10:n, function(i) {
dep <- y[1:i]
indep <- x[1:i]
fit <- lm(dep ~ indep)
pred <-
if (i < n) {
predict(fit, data.frame(indep = x[i+1L]), interval = "prediction")
} else NULL
list(fit = fit, pred = pred)
})
请注意,我正在制作一个 models/predictions 的列表,而不是使用 for
循环。尽管不完全相同,this answer 很好地解释了为什么这可能是个好主意。
其中一次运行的模型和预测:
ret[[50]]
# $fit
# Call:
# lm(formula = dep ~ indep)
# Coefficients:
# (Intercept) indep
# 0.44522 0.02691
# $pred
# fit lwr upr
# 1 0.4528911 -0.1160787 1.021861
summary(ret[[50]]$fit)
# Call:
# lm(formula = dep ~ indep)
# Residuals:
# Min 1Q Median 3Q Max
# -0.42619 -0.22178 -0.00004 0.15550 0.53774
# Coefficients:
# Estimate Std. Error t value Pr(>|t|)
# (Intercept) 0.44522 0.03667 12.141 <2e-16 ***
# indep 0.02691 0.03186 0.845 0.402
# ---
# Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Residual standard error: 0.2816 on 57 degrees of freedom
# Multiple R-squared: 0.01236, Adjusted R-squared: -0.004966
# F-statistic: 0.7134 on 1 and 57 DF, p-value: 0.4018