使用map2迭代两个参数(purrr函数)

时间:2019-01-26 00:24:16

标签: r binary probability logistic-regression

我想用多个模型计算出我的数据具有不同概率的所有可能的预测。结果是一个列表。

public function getId(){
    return $this->id;
}

public function getTodo(){
    return $this->todo;
}

然后我创建一个物流模型:

df<-iris
df$y<-sample(0:1,nrow(df),replace=TRUE)
set.seed(101)
#Now Selecting 80% of data as sample from total 'n' rows of the data  
sample <- sample.int(n = nrow(df), size = floor(.8*nrow(df)), replace = F)
train <- df[sample, ]
test  <- df[-sample, ]

首先,我有带有预测的表格。然后我创建了一个具有所有可能概率的向量。

 full <- glm(y~., data = train, family = "binomial")
 min <- glm( y~ 1, data = train, family = "binomial")

 backward <- step(full,direction = "backward",trace=0)
 forward <- step(min,scope=list(lower=min, upper=full),direction = "forward",trace=0)
model2<- glm(y~Sepal.Length+Sepal.Width , data = train, family = "binomial")

  models<-list(backward,forward,model2)
prediction<- lapply(models, function(x){predict(x,newdata=test,type="response")})

问题是我想应用差异中断点。我尝试了purrr包的map2函数,但不起作用。

    p <- seq(from = 0.1, to = 0.9, by = 0.5) 

问题是: 错误: pred = map2(prediction,p, function(x,pi){ifelse(x > pi, 1, 0)}) (3)和.x(2)的长度不同

有人可以帮忙吗?

我认为最好将更改应用于sapply,然后我将有一个data.frame。

.y

那我可以使用pmap函数吗? 谢谢

编辑:我更新了所有代码。

1 个答案:

答案 0 :(得分:1)

看看这是否有意义:

df<-iris
df$y<-sample(0:1,nrow(df),replace=TRUE)
set.seed(101)
#Now Selecting 80% of data as sample from total 'n' rows of the data  
sample <- sample.int(n = nrow(df), size = floor(.8*nrow(df)), replace = F)
train <- df[sample, ]
test  <- df[-sample, ]

full <- glm(y~., data = train, family = "binomial")
min <- glm( y~ 1, data = train, family = "binomial")

backward <- step(full,direction = "backward",trace=0)
forward <- step(min,scope=list(lower=min, upper=full),direction = "forward",trace=0)
model2<- glm(y~Sepal.Length+Sepal.Width , data = train, family = "binomial")

models<-list(backward,forward,model2)
prediction<- lapply(models, function(x){predict(x,newdata=test,type="response")})

p <- seq(from = 0.1, to = 0.9, by = 0.5) 

combn = cross2(prediction, p)

pred <- map(combn, 
            function(combination) {
              x <- combination[[1]]
              pi <- combination[[2]]
              ifelse(x > pi, 1, 0)
            }
)