All Questions
Tagged with probability r
689
questions
41
votes
4
answers
174k
views
R: Calculate and interpret odds ratio in logistic regression
I am having trouble interpreting the results of a logistic regression. My outcome variable is Decision and is binary (0 or 1, not take or take a product, respectively).
My predictor variable is ...
26
votes
4
answers
43k
views
Confidence interval for binomial data in R?
I know that I need mean and s.d to find the interval, however, what if the question is:
For a survey of 1,000 randomly chosen workers, 520 of them are female. Create a 95% confidence interval for the ...
26
votes
3
answers
22k
views
How do I calculate the probability for a given quantile in R?
Using R, it is trivial to calculate the quantiles for given probabilities in a sampled distribution:
x <- rnorm(1000, mean=4, sd=2)
quantile(x, .9) # results in 6.705755
However, I can't find an ...
25
votes
4
answers
13k
views
Is there a Python equivalent to R's sample() function?
I want to know if Python has an equivalent to the sample() function in R.
The sample() function takes a sample of the specified size from the elements of x using either with or without replacement.
...
22
votes
1
answer
15k
views
How to simulate bimodal distribution?
I have the following code to generate bimodal distribution but when I graph the histogram. I don't see the 2 modes. I am wondering if there's something wrong with my code.
mu1 <- log(1)
mu2 &...
15
votes
4
answers
12k
views
Divide each each cell of large matrix by sum of its row
I have a site by species matrix. The dimensions are 375 x 360. Each value represents the frequency of a species in samples of that site.
I am trying to convert this matrix from frequencies to ...
14
votes
2
answers
3k
views
Plot probability heatmap/hexbin with different sized bins
This is related to another question: Plot weighted frequency matrix.
I have this graphic (produced by the code below in R):
#Set the number of bets and number of trials and % lines
numbet <- 36
...
13
votes
3
answers
1k
views
Plot weighted frequency matrix
This question is related to two different questions I have asked previously:
1) Reproduce frequency matrix plot
2) Add 95% confidence limits to cumulative plot
I wish to reproduce this plot in R:
...
12
votes
3
answers
10k
views
Find the probability density of a new data point using "density" function in R
I am trying to find the best PDF of a continuous data that has unknown distribution, using the "density" function in R. Now, given a new data point, I want to find the probability density of this data ...
11
votes
2
answers
12k
views
Compute area under density estimation curve, i.e., probability
I have a density estimate (using density function) for my data learningTime (see figure below), and I need to find probability Pr(learningTime > c), i.e., the the area under density curve from a ...
10
votes
3
answers
34k
views
Getting Probability Density of Data
I need to analyze some data about internet sessions for a DSL Line. I wanted to have a look at how the session durations are distributed. I figured a simple way to do this would be to begin by making ...
9
votes
2
answers
537
views
Student's t-distribution CDF R base documentation
In the context of the Student's t-distribution cumulative distribution function, R Version 4.3.1's ?dt documentation highlights the following result:
However, upon attempting to verify the accuracy ...
9
votes
7
answers
24k
views
Predict function error for probabilities in glmnet?
I am trying to predict probabilities in a dataset using glmnet. My code reads:
bank <- read.table("http://www.stat.columbia.edu/~madigan/W2025/data/BankSortedMissing.TXT",header=TRUE)
bank$rich<...
8
votes
3
answers
6k
views
Finding stationary distribution of a markov process given a transition probability matrix
There has been two threads related to this issue on Stack Overflow:
How can I obtain stationary distribution of a Markov Chain given a transition probability matrix describes what a transition ...
8
votes
3
answers
2k
views
Using bnlearn Function "cpquery" Within a Loop
I'm attempting to use the bnlearn package to calculate conditional probabilities, and I'm running into a problem when the "cpquery" function is used within a loop. I've created an example, shown below,...
7
votes
3
answers
2k
views
Assigning a specific number of values informed by a probability distribution (in R)
Hello and thanks in advance for the help!
I am trying to generate a vector with a specific number of values that are assigned according to a probability distribution. For example, I want a vector of ...
7
votes
2
answers
3k
views
Probability of the Union of Three or More Sets
Consider the following sets of probabilities (the three events are NOT mutually exclusive):
0.05625 success, 0.94375 failure
0.05625 success, 0.94375 failure
0.05625 success, 0.94375 failure
How do ...
7
votes
1
answer
2k
views
Why do the inverse t-distributions for small values differ in Matlab and R?
I would like to evaluate the inverse Student's t-distribution function for small values, e.g., 1e-18, in Matlab. The degrees of freedom is 2.
Unfortunately, Matlab returns NaN:
tinv(1e-18,2)
NaN
...
7
votes
2
answers
6k
views
Generate a matrix of all possible outcomes for throwing n dice (ignoring order)
In cases where order does matter, it's rather easy to generate the matrix of all possible outcomes. One way for doing this is using expand.grid as shown here.
What if it doesn't?
If I'm right, the ...
7
votes
1
answer
13k
views
Multinomial regression using multinom function in R
I was thinking about posting my question in Cross-Validated, but decided to come here. I am using the multinom() function from the nnet package to estimate the odds of becoming employed, unemployed, ...
7
votes
2
answers
273
views
How to determine the number of possible combinations of letters that contain a degenerate substring
I've been racking my brain for a couple of days to work out a series or closed-form equation to the following problem:
Specifically: given all strings of length N that draws from an alphabet of L ...
6
votes
2
answers
14k
views
R : function to generate a mixture distribution
I need to generate samples from a mixed distribution
40% samples come from Gaussian(mean=2,sd=8)
20% samples come from Cauchy(location=25,scale=2)
40% samples come from Gaussian(mean = 10, sd=6)
...
6
votes
1
answer
13k
views
Kolmogorov-Smirnov test
I'm using the R function ks.test() to test the Uniform distribution of the R random number generator. I'm using the following code:
replicate(100000, ks.test(runif(n),y="punif").
When n is less than ...
6
votes
2
answers
2k
views
Generate stochastic random deviates from a density object with R
I have a density object dd created like this:
x1 <- rnorm(1000)
x2 <- rnorm(1000, 3, 2)
x <- rbind(x1, x2)
dd <- density(x)
plot(dd)
Which produces this very non-Gaussian distribution:...
6
votes
1
answer
2k
views
what is the "random" or non-deterministic factor inside SVM prediction by probabilities in e1071 in R?
I'm new to SVM and e1071. I found that the results are different every time I run the exact same code.
For example:
data(iris)
library(e1071)
model <- svm(Species ~ ., data = iris[-150,], ...
6
votes
1
answer
101
views
Comparison of two vectors resulted after simulation
I would like to apply the Rejection sampling method to simulate a random vector Y=(Y_1, Y_2) of a uniform distribution from a unit disc D = { (X_1 , X_2) \in R^2: \sqrt{x^2_1 + x^2_2} ≤ 1} such that X ...
6
votes
0
answers
3k
views
probability and classification in svm function of e1071 package in R
I'm using SVM in e1071 package for binary classification.
I'm using both the probability attribute, and the SVM predict classification to compare the results. What I'm puzzled by is that the predicted ...
6
votes
0
answers
2k
views
Flip a probability tree in R
Currently taking a course on decision analysis. There is a concept called "flipping a probability tree", illustrated in the following example.
May I know:
(1) Is there a way to do the above ...
5
votes
1
answer
2k
views
Probabilty heatmap in ggplot
I asked this question a year ago and got code for this "probability heatmap":
numbet <- 32
numtri <- 1e5
prob=5/6
#Fill a matrix
xcum <- matrix(NA, nrow=numtri, ncol=numbet+1)
for (i in 1:...
5
votes
4
answers
3k
views
How do i split a number randomly into multiple numbers given the number and n groups?
For instance , if the number is 100 and the number of groups is 4 it should give any random list of 4 numbers that add upto 100:
input number = 100
number of groups = 4
Possible outputs:
25, 25, ...
5
votes
2
answers
6k
views
R sample probabilities: Default is equal weight; why does specifying equal weights cause different values to be returned?
I have a simple question regarding the sample function in R. I'm randomly sampling from 0s and 1s and summing them together, from an input vector of length 5, which designates the number of trials to ...
5
votes
1
answer
10k
views
How to predict survival probabilities in R?
I have data called veteran stored in R. I created a survival model and now wish to predict survival probability predictions. For example, what is the probability that a patient with 80 karno value, ...
5
votes
3
answers
13k
views
Simulating Dice Rolls in R
I am trying to code the following game in R:
Roll a dice until you observe a 4 followed by a 6
Count how many times it took you to observe a 4 followed by a 6
Repeat these first two steps 100 times
...
5
votes
2
answers
796
views
How can I find out how many rows of a matrix satisfy a rather complicated criterion (in R)?
As an example, here is a way to get a matrix of all possible outcomes of rolling 4 (fair) dice.
z <- as.matrix(expand.grid(c(1:6),c(1:6),c(1:6),c(1:6)))
As you may already have understood, I'm ...
5
votes
1
answer
4k
views
R: monte carlo integration using Importance Sampling
I have an integral to evaluate
"x^(-0.5)" ; x in [0.01,1]
for which I am using Importance Sampling MC :
The theory says that an approximate PDF has to be used to compute the expected value (...
4
votes
7
answers
9k
views
How can I compute the probability at a point given a normal distribution in Perl?
Is there a package in Perl that allows you to compute the height of probability distribution at each given point. For example this can be done in R this way:
> dnorm(0, mean=4,sd=10)
> 0....
4
votes
3
answers
2k
views
Simulate coin toss for one week?
This is not homework. I am interested in setting up a simulation of a coin toss in R. I would like to run the simulation for a week. Is there a function in R that will allow me to start and stop the ...
4
votes
3
answers
377
views
Efficiently apply sample() in R
I need to sample an outcome variable given a matrix with row-wise outcome probabilities.
set.seed(1010) #reproducibility
#create a matrix of probabilities
#three possible outcomes, 10.000 cases
...
4
votes
3
answers
5k
views
Simulate data from (non-standard) density function
I want to simulate data from a non-standard density function. I already found the following link (How do I best simulate an arbitrary univariate random variate using its probability function?). ...
4
votes
3
answers
266
views
efficiently approximate real solution for polynomial function
I want to efficiently solve a degree-7 polynomial in k.
For example, with the following set of 7 unconditional probabilities,
p <- c(0.0496772, 0.04584501, 0.04210299, 0.04026439, 0.03844668, 0....
4
votes
1
answer
4k
views
Sample from the Erlang distribution in R
The Erlang distribution has two parameters: natural number k (shape parameter) and real number lambda (rate parameter). How can you take a random sample of size n from the Erlang distribution, using R ...
4
votes
1
answer
19k
views
R error: Error in `row.names<-.data.frame`(`*tmp*`, value = value)
I just make up a data set to test the function "mlogit" which stands for "multinomial logistic regression model"
The data is simply:
head(dat)
y x1 x2 x3
1 4 1 18 4
2 5 1 20 5
3 2 1 25 3
...
4
votes
2
answers
235
views
Monty Hall game in R with base functions
Just for fun and to train R, I tried to proof the Monty Hall Game rule (changing your choice after one gate opened gives you more probability to win), I made this reproducible code (The explanation of ...
4
votes
1
answer
322
views
Monte Carlo simulation in R for Monty Hall problem not working?
I'm writing a function in R to perform a Monte Carlo simulation for the Monty Hall problem. The function is working when the doors are not switched it switch == FALSE, but when I call mean(replicate(...
4
votes
1
answer
2k
views
Simulating data in R with multiple probability distributions
I am trying to simulate data via bootstrapping to create confidence bands for my real data with a funnel plot. I am building on the strategy of the accepted answer to a previous question. Instead of ...
4
votes
1
answer
290
views
How to calculated DRPS (Discrete Rank Probability Score)
I am working on replicating the scoring rule found in a paper Forecasting the intermittent demand for slow-moving inventories: A modelling approach
The paper describes the scoring rule as follows:
...
4
votes
1
answer
313
views
Exponential distribution simulation in R
I have the following graph:
I am told the following information:
(1) vertex A to vertex X is described by an exponential distribution with lambda = 4;
(2) vertex A to vertex Y is described by an ...
4
votes
2
answers
854
views
How to deal with the log of a sum of more than two super small probabilities
For some evil reason I need to calculate the log of the sum of 500 super small probabilities, each term computed by
dmvnorm(X[,i], mean=rep(0,3), sigma=diag(3))
Sometimes the codes above return 0 ...
4
votes
1
answer
833
views
Balls in Bins simulation for R
I would like to simulate the distribution for a fixed number of balls m in a fixed number of bins n in R. Up till now I have been using the Poisson approximation with rpois(). This is a decent ...
4
votes
1
answer
1k
views
R: calculating the probability of drawing at least 1 red marble
Suppose my population has n marbles, and only 1% of them are red. In a sample of 30 draws, what's the probability that I draw at least 1 red marble?
I know that P(at least 1 red marble) = 1 - P(no ...