如何使用 poly R 函数更改 for 循环中的多项式阶数?
How to change the polynomial order in a for-loop using the poly R function?
我想通过尝试不同的多项式来拟合回归,并且我尝试了 运行 这个循环:
for (p_order in 1:9) {
assign(paste("RD0", p_order, sep = ""), electricity_price ~ d1 + gas_price + coal_price +
oil_price + EUA + weekday + month + median_windspeed1 +
median_windspeed2 + median_windspeed3 + median_windspeed4 +
sun1 + sun2 + sun3 + sun4 + median_temp1 + median_temp2 +
median_temp3 + median_temp4 + poly(as.numeric(date), p_order, raw=TRUE) + time)
}
虽然它正确地创建了变量的名称(RD01、RD02 等),但它没有保存多项式的正确顺序(1,2 等),而是存储 "p_order" 。例如,
> RD04
electricity_price ~ d1 + gas_price + coal_price + oil_price +
EUA + weekday + month + median_windspeed1 + median_windspeed2 +
median_windspeed3 + median_windspeed4 + sun1 + sun2 + sun3 +
sun4 + median_temp1 + median_temp2 + median_temp3 + median_temp4 +
poly(as.numeric(date), p_order, raw = TRUE) + time
> RD07
electricity_price ~ d1 + gas_price + coal_price + oil_price +
EUA + weekday + month + median_windspeed1 + median_windspeed2 +
median_windspeed3 + median_windspeed4 + sun1 + sun2 + sun3 +
sun4 + median_temp1 + median_temp2 + median_temp3 + median_temp4 +
poly(as.numeric(date), p_order, raw = TRUE) + time
有人可以向我解释为什么以及如何解决这个问题吗?
谢谢!
先创建公式,再赋值。我会post一个简化的例子。
for (p_order in 1:2) {
fmla <- paste("electricity_price ~ d1 + poly(as.numeric(date),", p_order, ", raw = TRUE)")
assign(paste("RD0", p_order, sep = ""), as.formula(fmla))
}
RD01
#electricity_price ~ d1 + poly(as.numeric(date), 1, raw = TRUE)
RD02
#electricity_price ~ d1 + poly(as.numeric(date), 2, raw = TRUE)
我想通过尝试不同的多项式来拟合回归,并且我尝试了 运行 这个循环:
for (p_order in 1:9) {
assign(paste("RD0", p_order, sep = ""), electricity_price ~ d1 + gas_price + coal_price +
oil_price + EUA + weekday + month + median_windspeed1 +
median_windspeed2 + median_windspeed3 + median_windspeed4 +
sun1 + sun2 + sun3 + sun4 + median_temp1 + median_temp2 +
median_temp3 + median_temp4 + poly(as.numeric(date), p_order, raw=TRUE) + time)
}
虽然它正确地创建了变量的名称(RD01、RD02 等),但它没有保存多项式的正确顺序(1,2 等),而是存储 "p_order" 。例如,
> RD04
electricity_price ~ d1 + gas_price + coal_price + oil_price +
EUA + weekday + month + median_windspeed1 + median_windspeed2 +
median_windspeed3 + median_windspeed4 + sun1 + sun2 + sun3 +
sun4 + median_temp1 + median_temp2 + median_temp3 + median_temp4 +
poly(as.numeric(date), p_order, raw = TRUE) + time
> RD07
electricity_price ~ d1 + gas_price + coal_price + oil_price +
EUA + weekday + month + median_windspeed1 + median_windspeed2 +
median_windspeed3 + median_windspeed4 + sun1 + sun2 + sun3 +
sun4 + median_temp1 + median_temp2 + median_temp3 + median_temp4 +
poly(as.numeric(date), p_order, raw = TRUE) + time
有人可以向我解释为什么以及如何解决这个问题吗?
谢谢!
先创建公式,再赋值。我会post一个简化的例子。
for (p_order in 1:2) {
fmla <- paste("electricity_price ~ d1 + poly(as.numeric(date),", p_order, ", raw = TRUE)")
assign(paste("RD0", p_order, sep = ""), as.formula(fmla))
}
RD01
#electricity_price ~ d1 + poly(as.numeric(date), 1, raw = TRUE)
RD02
#electricity_price ~ d1 + poly(as.numeric(date), 2, raw = TRUE)