Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据R中的一组规则进行预测_R_Key_Rule - Fatal编程技术网

如何根据R中的一组规则进行预测

如何根据R中的一组规则进行预测,r,key,rule,R,Key,Rule,我有一个列表,其中包含一组规则(或键或字典;随便你怎么称呼它) >清单.问题[c(1,2)] [[1]] x否是 多云0.07692308 0.42857143 多雨0.38461538 0.33333 晴朗0.53846154 0.23809524 因此,给定“否”的阴天值为-0.08,给定“是”的阴天值为0.43 [[2]] y x no yes cool 0.2307692 0.3333333 hot 0.3846154 0.

我有一个列表,其中包含一组规则(或键或字典;随便你怎么称呼它)

>清单.问题[c(1,2)] [[1]] x否是 多云0.07692308 0.42857143 多雨0.38461538 0.33333 晴朗0.53846154 0.23809524 因此,给定“否”的阴天值为-0.08,给定“是”的阴天值为0.43

[[2]] y x no yes cool 0.2307692 0.3333333 hot 0.3846154 0.2380952 mild 0.3846154 0.4285714 [[2]] Y x否是 冷却0.2307692 0.3333333 热态0.3846154 0.2380952 轻度0.3846154 0.4285714 同样,给定的“否”的值为0.38,给定的“是”的值为0.24

一旦建立了规则,我就有了一个字符矩阵

> mat[c(1:4),] outlook temperature humidity windy [1,] "sunny" "hot" "high" "no" [2,] "sunny" "hot" "high" "yes" [3,] "overcast" "hot" "high" "no" [4,] "rainy" "mild" "high" "no" >mat[c(1:4),] 展望温湿度多风 [1,]“阳光”“炎热”“高”“否” [2,]“阳光”“炎热”“高”“是” [3,]“阴”“热”“高”“否” [4,]“多雨”“轻度”“高”“否”
问题是“否”或“是”,我如何使用先前的规则并将矩阵中的单元格(存储为字符)转换为相应的数值。

这是否符合您的要求?我不确定是/否在哪里起作用,所以我只是查了一下“是”的概率


a你的例子是不可复制的,所以我试着这样做:

list.prob <-
list(structure(c(0.07692308, 0.38461538, 0.53846154, 0.42857143, 
0.33333333, 0.23809524), .Dim = c(3L, 2L), .Dimnames = structure(list(
    x = c("overcast", "rainy", "sunny"), y = c("no", "yes")), .Names = c("x", 
"y"))), structure(c(0.2307692, 0.3846154, 0.3846154, 0.3333333, 
0.2380952, 0.4285714), .Dim = c(3L, 2L), .Dimnames = structure(list(
    x = c("cool", "hot", "mild"), y = c("no", "yes")), .Names = c("x", 
"y"))))
mat <-
structure(c("sunny", "sunny", "overcast", "rainy", "hot", "hot", 
"hot", "mild"), .Dim = c(4L, 2L), .Dimnames = list(NULL, c("outlook", 
"temperature")))
然后,您的问题是根据
list.prob
中的表翻译
mat
中的每一列,但使用哪一列取决于另一个变量的值(我将其称为
yesorno

这假设
mat
的第一列对应于
列表的第一个元素。prob
等。;如果映射不是那么简单,那么您需要某种关联它们的方式(可能根据
mat
的列名命名
list.prob
的元素,并循环列名在
list.prob
中进行查找)

a  <- matrix(runif(6), nrow = 3)
weather <- c("sunny", "rainy", "overcast")
temp <- c("cool", "hot", "mild")
yn <- c("yes", "no")
rownames(a) <- weather
colnames(a) <- yn
b  <- matrix(runif(6), nrow = 3)
rownames(b) <- temp
colnames(b) <- yn
c <- data.frame(weather = sample(weather, 10, replace = T), 
     temp = sample(temp, 10, replace = T))
d <- data.frame(weather = a[c$weather, "yes"], temp = b[c$temp, "yes"])
a
b
c
d
list.prob <-
list(structure(c(0.07692308, 0.38461538, 0.53846154, 0.42857143, 
0.33333333, 0.23809524), .Dim = c(3L, 2L), .Dimnames = structure(list(
    x = c("overcast", "rainy", "sunny"), y = c("no", "yes")), .Names = c("x", 
"y"))), structure(c(0.2307692, 0.3846154, 0.3846154, 0.3333333, 
0.2380952, 0.4285714), .Dim = c(3L, 2L), .Dimnames = structure(list(
    x = c("cool", "hot", "mild"), y = c("no", "yes")), .Names = c("x", 
"y"))))
mat <-
structure(c("sunny", "sunny", "overcast", "rainy", "hot", "hot", 
"hot", "mild"), .Dim = c(4L, 2L), .Dimnames = list(NULL, c("outlook", 
"temperature")))
> list.prob
[[1]]
          y
x                  no       yes
  overcast 0.07692308 0.4285714
  rainy    0.38461538 0.3333333
  sunny    0.53846154 0.2380952

[[2]]
      y
x             no       yes
  cool 0.2307692 0.3333333
  hot  0.3846154 0.2380952
  mild 0.3846154 0.4285714

> mat
     outlook    temperature
[1,] "sunny"    "hot"      
[2,] "sunny"    "hot"      
[3,] "overcast" "hot"      
[4,] "rainy"    "mild"     
# setup
res <- matrix(0,nrow=nrow(mat),ncol=ncol(mat))
yesorno <- "yes"
# actual computation
for (col in seq_len(ncol(mat))) {
  res[,col] <- list.prob[[col]][,yesorno][mat[,col]]
}
> res
          [,1]      [,2]
[1,] 0.2380952 0.2380952
[2,] 0.2380952 0.2380952
[3,] 0.4285714 0.2380952
[4,] 0.3333333 0.4285714