Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8504317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:59:41+00:00 2026-06-11T01:59:41+00:00

I am writing a function in R that will compute a sum of squares

  • 0

I am writing a function in R that will compute a sum of squares between a binomial distribution and normal distribution and display the data as a function of p

Here is what I have:

First I generate a random binomial distribution with probability p (n=100)

random_binom<-rbinom(100,100,p)

Next, I find the probability that some random element has equal or less than the value np (the mean) and the standard deviation of sqrt(np(1-p)) according to the normal distribution

d_norm<-dnorm(random_binom,p*100,sqrt(100*p*(1-p)))

And then the probability that a random value in the distribution will have equal or less than the value of the mean according to the binomial distribution

d_binom<-dbinom(random_binom,100,p)

finally I subtract the two, take the square and return it.

result<-sum((d_norm-d_binom)^2)
return(result)

Now within the console, I created a function for all this:

myfunction:

function(p){
random_binom<-rbinom(100,100,p)
d_norm<-dnorm(random_binom,p*100,sqrt(100*p*(1-p)))
d_binom<-dbinom(random_binom,100,p)
result<-sum((d_norm-d_binom)^2)
return(result)
}

I want to pass in a vector for p where p<-(c(0:99)/100), but whenever I do that, the function returns

inf

rather than a vector of values. How can I get R to return a vector of values so that I can plot them? I have tried using lapply but that returns the data in a strange format:

[[95]]
[1] 0.01064091

[[96]]
[1] 0.01418807

[[97]]
[1] 0.02647295

[[98]]
[1] 0.05065813

[[99]]
[1] 0.1179141

[[100]]
[1] 0.7342808

meaning each element is contained another element, making it very difficult to graph.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:59:43+00:00Added an answer on June 11, 2026 at 1:59 am

    The problem is that when p is 0, your function will effectively do this:

    dnorm(0, 0, 0)
    [1] Inf
    

    Maybe you want p<-(c(1:99)/100)?


    lapply returns a list. You can use unlist to convert to an array. Suppose you assign the results of an lapply call to L. Then, you can do this

    unlist(L)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some C++ code that will have to send data over TCP/IP. I
I am currently writing a MEX function that will have to work with a
I am writing a Function that has a return-type which will dynamically change depending
I'm writing a program that sends an array through a function (upperhess) that will
How would i go about writing a function that would handle the data in
I am writing a function that will take a parameter and, among other things,
I am writing a native function that will return multiple Python objects PyObject *V
I am writing a PHP function that will need to loop over an array
Say am writing a function that will add a product to the shopping cart.
I'm writing a function that will be passed a lambda expression, and I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.