Issue
I am working with the R programming language.
I wrote the following code that generates 20 random points from a Normal Distribution and then plots the likelihood function:
# generate random data
x1 = rnorm(1,5,5)
x2 = rnorm(1,5,5)
x3 = rnorm(1,5,5)
x4 = rnorm(1,5,5)
x5 = rnorm(1,5,5)
x6 = rnorm(1,5,5)
x7 = rnorm(1,5,5)
x8 = rnorm(1,5,5)
x9 = rnorm(1,5,5)
x10 = rnorm(1,5,5)
x11 = rnorm(1,5,5)
x12 = rnorm(1,5,5)
x13 = rnorm(1,5,5)
x14 = rnorm(1,5,5)
x15 = rnorm(1,5,5)
x16 = rnorm(1,5,5)
x17 = rnorm(1,5,5)
x18 = rnorm(1,5,5)
x19 = rnorm(1,5,5)
x20 = rnorm(1,5,5)
# Define Likelihood Function (from here: #https://www.statlect.com/fundamentalsofstatistics/normaldistributionmaximumlikelihood  I broke the Likelihood Function into 4 parts "a", "b", "c", "d" : then I added them together to make the full Likelihood Function "f")
my_function < function(mu,sigma) {
n = 20
a = n/2*log(2*pi)
b = n/2*log(sigma^2)
c = 1/(2*sigma^2)
d = (x1mu)^2 + (x2mu)^2 + (x3mu)^2 + (x4mu)^2 + (x5mu)^2 + (x6mu)^2 + (x7mu)^2 + (x8mu)^2 + (x9mu)^2 + (x10mu)^2 + (x11mu)^2 + (x12mu)^2 + (x13mu)^2 + (x14mu)^2 + (x15mu)^2 + (x16mu)^2 + (x17mu)^2 + (x18mu)^2 + (x19mu)^2 + (x20mu)^2
f = a + b + c + d
}
# plot results
library(plotly)
input_1 < seq(20, 20,0.1)
input_2 < seq(20,20, 0.1)
z < outer(input_1, input_2, my_function)
plot_ly(x = input_1, y = input_2, z = z) %>% add_surface()
My Question: Can someone please show me how to make a more "efficient" version of this code?

For example, I had thought of directly generating 20 points from this distribution in one shot
my_data = rnorm(20,5,5)
and then place them into a data frame but I did not know how to "feed" data from a data frame into the function 
Since I had 20 data points, I had to manually write
(x_i mu)^2
20 different times – would it have been possible to have a function that could have "recognized" that there were 20 points and then "adapted" itself to "accommodate" these 20 points without having to manually rewrite(x_i mu)^2
so many times?
Solution
For storing data into the data.frame you can just do the following:
my_data < rnorm(20, 5, 5)
df < data.frame(x = my_data, <other_columns>)
Note, the other columns need to be of the same length. If this is not the case, it would be better to keep these numbers separate.
Then within the my_function
, you can have:
d = sum((df$my_data  mu)^2)
If you are not using dataframe, this would be instead:
d = sum((my_data  mu)^2)
Answered By – AdroMine
Answer Checked By – Robin (BugsFixing Admin)