1
$\begingroup$

I need to do a Bernoulli test in R.

Lets say i have a set of data specified to insert (10 random samples of 40). From this there are four possible options, and i wish to assign the likelihood of which is chosen based on their means and sd? the randomly generated samples come from a normal distribution with mean of 25 and sd of 4.

I'm just unsure exactly how to code this in R. Thanks for any help.

$\endgroup$
2
  • 2
    $\begingroup$ Could you please clarify this question? What kind of data do you have? What are you trying to learn about them? Where do the 0.3 and 0.7 come from? $\endgroup$
    – whuber
    Commented Jul 28, 2012 at 19:01
  • 3
    $\begingroup$ I don't get it, are you asking a new question here? Do you want to know how to do a one sample t-test in R? How are normal data w/ mean=25 & SD=4 related to a Bernoulli? If this is a totally different question, it would be best to start a new question, instead of deleting most of your old one. If the older phrasing let to a misunderstanding & answers that aren't really what you need, we prefer you edit below the original text. $\endgroup$ Commented Jul 28, 2012 at 20:10

1 Answer 1

5
$\begingroup$

If you want to test that your sample is sampled from a Bernoulli distribution with parameter $p=0.3$ you need to count the number of successes (the 1s) and the size of the sample. Here is an example

# Simulate a random sample of size 17 with p=.3
set.seed(123)
simulated_sample <- runif(17) < 0.3
x <- sum(simulated_sample == TRUE)    # (number of successes)
n <- length(simulated_sample) # (sample size)
# Do the test.
binom.test(x, n, p=0.3)
#   Exact binomial test
# 
# data:  x and n 
# number of successes = 4, number of trials = 17, p-value = 0.7919
# alternative hypothesis: true probability of success is not equal to 0.3 
# 95 percent confidence interval:
#  0.06810774 0.49899327 
# sample estimates:
# probability of success 
#              0.2352941 
$\endgroup$
5
  • $\begingroup$ +1, clear & to the point. One question out of curiosity, any reason you didn't generate data w/ rbinom()? Seems like it would have been the default choice for me (although, I recognize there is nothing wrong w/ the way you did it). $\endgroup$ Commented Jul 28, 2012 at 19:28
  • $\begingroup$ @gung thanks! There is no special reason, it is just a programming habit (because I sometimes like to have series of success available). rbinom is probably more pedagogical, I will use it instead. $\endgroup$
    – gui11aume
    Commented Jul 28, 2012 at 19:33
  • $\begingroup$ @gung, thinking about it I will actually leave it as is for the demo. You usually have a real sample, from which you have to compute the number of successes and the length. Even if these are trivial operations in R I prefer to show them. $\endgroup$
    – gui11aume
    Commented Jul 28, 2012 at 19:37
  • $\begingroup$ Thanks for that. Lets say i have a set of data specified to insert in place of the random generated 17 (10 random samples of 40). From this there are four possible options, and i wish to assign the likelihood of which is chosen based on their means and sd? is this possible in R? $\endgroup$
    – YesSure
    Commented Jul 28, 2012 at 19:53
  • 2
    $\begingroup$ Yes of course. I suggest you follow @whuber's advice and edit your post to explain exactly what you want to do. You'll get more appropriate answers then. $\endgroup$
    – gui11aume
    Commented Jul 28, 2012 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.