完全删除 [R] 中的 NA
completely deleting NAs in [R]
我正在尝试在 R 中执行 VAR 函数。我有如下数据集。
library(vars)
`Riskp <- data.frame(dayahead2$Date, Risk, DF5$Load_DE, DF5$`DF2$MW_sol`, DF5$`DF3$MW_Wind`)`
> head(Riskp)
dayahead2.Date Risk DF5.Load_DE DF5..DF2.MW_sol. DF5..DF3.MW_Wind.
1 2011-01-02 26.91 43949 0 7396.450
2 2011-01-02 15.15 41708 0 7237.775
3 2011-01-02 13.17 40489 0 6956.850
4 2011-01-02 13.30 39711 0 6875.775
5 2011-01-02 15.20 39407 0 6599.275
6 2011-01-02 19.15 38394 0 6270.750
然后我完成了
Riskp1 <- na.omit(Riskp)
取出 NA。结果,从数据中取出 NA,如下所示。
来自这个:
str(Riskp)
'data.frame': 43800 obs. of 5 variables:
对此:
str(Riskp1)
'data.frame': 43787 obs. of 5 variables:
现在我想做VAR测试。通过这样做,比方说,VAR(Riskp1, type = "both", lag.max = 10, ic = "AIC")
.
但是显示'x'里面有NA/NaN/Inf。
> VAR(Riskp1,type="both",lag.max = 10,ic="AIC")
Error in lm.fit(x = ys.lagged, y = yendog) :
'x' 내에 NA/NaN/Inf가 있습니다 <- (meaning "there is NA/NaN/Inf in 'x'")
In addition: Warning messages:
1: In lm.fit(x = ys.lagged, y = yendog) : NAs introduced by coercion
2: In lm.fit(x = ys.lagged, y = yendog) : NAs introduced by coercion
我以为我删除了 NA。但似乎它仍然存在。如何彻底删除我的数据?
提前谢谢大家。
问题出在 Date
列。要使 VAR 函数处理日期,您需要提供一个 ts
对象。这是一个例子:
library(vars)
library(zoo)
df <- read.table(text = "dayahead2.Date Risk DF5.Load_DE DF5..DF2.MW_sol. DF5..DF3.MW_Wind.
1 2011-01-02 26.91 43949 0 7396.450
2 2011-01-02 15.15 41708 0 7237.775
3 2011-01-02 13.17 40489 0 6956.850
4 2011-01-02 13.30 39711 0 6875.775
5 2011-01-02 15.20 39407 0 6599.275
6 2011-01-02 19.15 38394 0 6270.750", header = T)
复制 df 10 次,因为 VAR
抛出与太少观察相关的错误:
df <- df[rep(1:nrow(df), times = 10),]
df$dayahead2.Date <- as.Date(df$dayahead2.Date) #convert to date
将数值列转换为矩阵
mat <- as.matrix(df[,2:ncol(df)])
转换为 ts
对象:
Zoo <- ts(zoo(mat, df$dayahead2.Date))
使用 VAR 函数:
VAR(Zoo, type="both",lag.max = 10,ic="AIC")
我正在尝试在 R 中执行 VAR 函数。我有如下数据集。
library(vars)
`Riskp <- data.frame(dayahead2$Date, Risk, DF5$Load_DE, DF5$`DF2$MW_sol`, DF5$`DF3$MW_Wind`)`
> head(Riskp)
dayahead2.Date Risk DF5.Load_DE DF5..DF2.MW_sol. DF5..DF3.MW_Wind.
1 2011-01-02 26.91 43949 0 7396.450
2 2011-01-02 15.15 41708 0 7237.775
3 2011-01-02 13.17 40489 0 6956.850
4 2011-01-02 13.30 39711 0 6875.775
5 2011-01-02 15.20 39407 0 6599.275
6 2011-01-02 19.15 38394 0 6270.750
然后我完成了
Riskp1 <- na.omit(Riskp)
取出 NA。结果,从数据中取出 NA,如下所示。
来自这个:
str(Riskp)
'data.frame': 43800 obs. of 5 variables:
对此:
str(Riskp1)
'data.frame': 43787 obs. of 5 variables:
现在我想做VAR测试。通过这样做,比方说,VAR(Riskp1, type = "both", lag.max = 10, ic = "AIC")
.
但是显示'x'里面有NA/NaN/Inf。
> VAR(Riskp1,type="both",lag.max = 10,ic="AIC")
Error in lm.fit(x = ys.lagged, y = yendog) :
'x' 내에 NA/NaN/Inf가 있습니다 <- (meaning "there is NA/NaN/Inf in 'x'")
In addition: Warning messages:
1: In lm.fit(x = ys.lagged, y = yendog) : NAs introduced by coercion
2: In lm.fit(x = ys.lagged, y = yendog) : NAs introduced by coercion
我以为我删除了 NA。但似乎它仍然存在。如何彻底删除我的数据?
提前谢谢大家。
问题出在 Date
列。要使 VAR 函数处理日期,您需要提供一个 ts
对象。这是一个例子:
library(vars)
library(zoo)
df <- read.table(text = "dayahead2.Date Risk DF5.Load_DE DF5..DF2.MW_sol. DF5..DF3.MW_Wind.
1 2011-01-02 26.91 43949 0 7396.450
2 2011-01-02 15.15 41708 0 7237.775
3 2011-01-02 13.17 40489 0 6956.850
4 2011-01-02 13.30 39711 0 6875.775
5 2011-01-02 15.20 39407 0 6599.275
6 2011-01-02 19.15 38394 0 6270.750", header = T)
复制 df 10 次,因为 VAR
抛出与太少观察相关的错误:
df <- df[rep(1:nrow(df), times = 10),]
df$dayahead2.Date <- as.Date(df$dayahead2.Date) #convert to date
将数值列转换为矩阵
mat <- as.matrix(df[,2:ncol(df)])
转换为 ts
对象:
Zoo <- ts(zoo(mat, df$dayahead2.Date))
使用 VAR 函数:
VAR(Zoo, type="both",lag.max = 10,ic="AIC")