如何生成具有预测概率的随机数据集? [英] How to generate random data set with predicted probability?
问题描述
我正在努力生成具有预测的多项式逻辑回归概率的随机数据集。
I'm struggling to generate random data set with predicted probability of multinomial logistic regression.
让我们举个例子。我将使用 nnet
软件包进行多项逻辑回归。我还将使用 rattle.data
包中的葡萄酒
数据集。
Let's take an example. I'll use nnet
package for multinomial logistic regression. I will also use wine
data set in rattle.data
package.
library("nnet")
library("rattle.data")
data(wine)
multinom.fit<-multinom(Type~Alcohol+Color,data=wine)
summary(multinom.fit)
Call:
multinom(formula = Type ~ Alcohol + Color - 1, data = wine)
Coefficients:
Alcohol Color
2 0.6258035 -1.9480658
3 -0.3457799 0.6944604
Std. Errors:
Alcohol Color
2 0.10203198 0.3204171
3 0.07042968 0.1479679
Residual Deviance: 222.5608
AIC: 230.5608
fit<-fitted(multinom.fit)
head(fit)
1 2 3
1 0.6705935 0.0836177621 0.24578870
2 0.5050334 0.3847919037 0.11017466
3 0.6232029 0.0367975986 0.33999948
4 0.3895445 0.0007888818 0.60966664
5 0.4797392 0.4212542898 0.09900655
6 0.5510792 0.0077589278 0.44116190
因此, fit
数据集为178 * 3数据框。我想使用预测的概率生成100个随机数据集。例如, fit
数据集中的第一个样本的概率为'1',概率为0.67,'2'为0.08,'3'为0.24。每个样本都是独立募集(收集?)的。
So, the fit
dataset is 178*3 dataframe. I want to generated 100 random dataset, using predicted probability. For example, the first sample in fit
dataset has about 0.67 probability to be '1' and 0.08 to '2', 0.24 to '3'. Each sample was recruited(collected?) independently.
有没有一种执行方法?
推荐答案
您可以尝试:
rand.list <- lapply(1:nrow(fit), function(x) sample(1:3, 100, replace = TRUE, prob = fit[x, ]))
rand.df <- data.frame(matrix(unlist(rand.list), ncol = nrow(fit)))
它将为您提供一个包含100个观察值和178列的data.frame在 fit
中每一行具有不同的采样概率。
It will give you a data.frame with 100 observations and 178 columns with the different sampling probabilities of each row in fit
.
这篇关于如何生成具有预测概率的随机数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!