数据框中的非数字变量?我该如何解决?
我目前正在从事电视VAR应用程序以进行预测。不幸的是,我是R半脚。
我的目的是:我想使用自2018-01-01以来的天数来选择一个好的带宽参数。
不知何故,我会收到此错误:
** r在Math.data.frame中
这是我执行的代码和列表:
首先是软件包和列表:
install.packages("mgm")
df_list_copy <- structure(list(`Survey Creation Date` = c("2/6/2018 14:33", "2/6/2018 16:20",
"2/6/2018 18:54", "2/6/2018 20:08", "2/6/2018 22:29", "2/7/2018 8:43",
"2/7/2018 10:52", "2/7/2018 12:21", "2/7/2018 14:56", "2/7/2018 16:20",
"2/7/2018 18:27", "2/7/2018 20:11", "2/7/2018 22:15", "2/8/2018 8:34",
"2/8/2018 10:36", "2/8/2018 12:34", "2/8/2018 14:28", "2/8/2018 16:44",
"2/8/2018 18:41", "2/8/2018 20:24"), `Survey Completion Date` = c("2/6/2018 14:56",
"2/6/2018 16:22", "2/6/2018 18:58", "2/6/2018 20:22", "2/6/2018 22:46",
"2/7/2018 8:44", "2/7/2018 11:23", "2/7/2018 12:26", "2/7/2018 14:58",
"2/7/2018 16:21", "2/7/2018 19:55", "2/7/2018 20:13", "2/7/2018 22:16",
"2/8/2018 9:41", "2/8/2018 11:05", "2/8/2018 12:54", "2/8/2018 14:31",
NA, "2/8/2018 19:52", "2/8/2018 20:41"), `Since your last survey; how many alcoholic drinks have you had?` = c(0,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 0, NA, 2, 5), `I feel comfortable in my current location` = c(88,
81, 88, 89, 95, 94, 62, 82, 63, 80, 79, 81, 95, 100, 88, 83,
50, NA, 72, 61), `I feel stressed` = c(10, 12, 69, 34, 16, 6,
27, 35, 56, 28, 58, 20, 32, 10, 26, 43, 44, NA, 57, 48), `I feel down/depressed` = c(14,
18, 15, 18, 5, 2, 8, 4, 0, 11, 13, 10, 4, 6, 2, 7, 0, NA, 0,
0)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))
这是将time_date分解为dayno和beep(一天观察的数量),
## Create time date table with n of observations per day and days of observation since 2018-01-01
time_date <- df_list_copy %>%
mutate(Date = as.Date(`Survey Creation Date`, format = "%m/%d/%Y %H:%M")) %>%
group_by(Date) %>%
count() %>%
mutate(Days_of_observation = Date - as.Date("2018-01-01"))
Beep <- time_date[2]
Dayno <- time_date[3]
bwSeq <- seq(0.01, 1, length = 10)
这是我收到错误的地方
set.seed(1)
bw_object <- bwSelect(data = df_list_copy,
type = rep("g", 12),
level = rep(1, 12),
bwSeq = bwSeq,
bwFolds = 1,
bwFoldsize = 10,
modeltype = "mvar",
lags = 1,
scale = TRUE,
beepvar = Beep,
dayvar = Dayno,
pbar = TRUE)
I am currently working on tv var applications for predictions. Unfortunately, I am an R semi-beginner.
My aim: I want to use the number of days since 2018-01-01 to select a good bandwidth parameter.
Somehow I get this error:
**r in Math.data.frame(list(Days_of_observation = c(36, 37, 38, 39, : non-numeric-alike variable(s) in data frame: Days_of_observation**
This is the code and list I performed:
First the package and list:
install.packages("mgm")
df_list_copy <- structure(list(`Survey Creation Date` = c("2/6/2018 14:33", "2/6/2018 16:20",
"2/6/2018 18:54", "2/6/2018 20:08", "2/6/2018 22:29", "2/7/2018 8:43",
"2/7/2018 10:52", "2/7/2018 12:21", "2/7/2018 14:56", "2/7/2018 16:20",
"2/7/2018 18:27", "2/7/2018 20:11", "2/7/2018 22:15", "2/8/2018 8:34",
"2/8/2018 10:36", "2/8/2018 12:34", "2/8/2018 14:28", "2/8/2018 16:44",
"2/8/2018 18:41", "2/8/2018 20:24"), `Survey Completion Date` = c("2/6/2018 14:56",
"2/6/2018 16:22", "2/6/2018 18:58", "2/6/2018 20:22", "2/6/2018 22:46",
"2/7/2018 8:44", "2/7/2018 11:23", "2/7/2018 12:26", "2/7/2018 14:58",
"2/7/2018 16:21", "2/7/2018 19:55", "2/7/2018 20:13", "2/7/2018 22:16",
"2/8/2018 9:41", "2/8/2018 11:05", "2/8/2018 12:54", "2/8/2018 14:31",
NA, "2/8/2018 19:52", "2/8/2018 20:41"), `Since your last survey; how many alcoholic drinks have you had?` = c(0,
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 6, 0, NA, 2, 5), `I feel comfortable in my current location` = c(88,
81, 88, 89, 95, 94, 62, 82, 63, 80, 79, 81, 95, 100, 88, 83,
50, NA, 72, 61), `I feel stressed` = c(10, 12, 69, 34, 16, 6,
27, 35, 56, 28, 58, 20, 32, 10, 26, 43, 44, NA, 57, 48), `I feel down/depressed` = c(14,
18, 15, 18, 5, 2, 8, 4, 0, 11, 13, 10, 4, 6, 2, 7, 0, NA, 0,
0)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))
This is the splitting of the time_date into dayno and beep (number of observations on one day)
## Create time date table with n of observations per day and days of observation since 2018-01-01
time_date <- df_list_copy %>%
mutate(Date = as.Date(`Survey Creation Date`, format = "%m/%d/%Y %H:%M")) %>%
group_by(Date) %>%
count() %>%
mutate(Days_of_observation = Date - as.Date("2018-01-01"))
Beep <- time_date[2]
Dayno <- time_date[3]
bwSeq <- seq(0.01, 1, length = 10)
This is where I receive the error
set.seed(1)
bw_object <- bwSelect(data = df_list_copy,
type = rep("g", 12),
level = rep(1, 12),
bwSeq = bwSeq,
bwFolds = 1,
bwFoldsize = 10,
modeltype = "mvar",
lags = 1,
scale = TRUE,
beepvar = Beep,
dayvar = Dayno,
pbar = TRUE)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码有很多问题。
参数 data 在使用tibble对象时应为AnxP矩阵对象。
r中的矩阵对象仅包含一种类型的数据,而参数 data 应包含数字数据,我认为您不能直接将时间戳与数字对象一起使用。<<<<<<<<<<<<<< /p>
参数 type 是AP向量(在您的情况下,它应该是长度为6的向量),而您使用了长度为12的向量。
grognm长度向量在使用长度为12的矢量时。
我的调试结果,我认为参数 beepvar 和 dayvar >应该是向量而不是tibble。
从我的调试结果中,我认为参数 data 应该是一个不包含na值的矩阵。
可能还有其他问题。我强烈建议您首先了解函数
bwselect()
,然后检查bwselect()
的演示代码。Your code has many issues.
Argument data should be a n x p matrix object while you have used tibble object.
Given the matrix object in R contains only one type of data and the argument data should contain numeric data, I don't think you can directly use the time stamp together with the numeric object.
Argument type is a p vector(In your case, it should be a vector of length 6) while you have used a vector of length 12.
Argument level is a p length vector while you have used a vector of length 12.
From my debugging result, I think argument beepvar and dayvar should be a vector rather than a tibble.
From my debugging result, I think argument data should be a matrix that doesn't contain NA value in your case.
There may be some other issues. I strongly recommend you to understand the function
bwSelect()
first and check the demo code ofbwSelect()
.