# [SOLVED] R: Adapting a Function Based on the Size of a Data Frame

## 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/fundamentals-of-statistics/normal-distribution-maximum-likelihood - 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 = (x1-mu)^2 + (x2-mu)^2 + (x3-mu)^2 + (x4-mu)^2 + (x5-mu)^2 + (x6-mu)^2 + (x7-mu)^2 + (x8-mu)^2 + (x9-mu)^2 + (x10-mu)^2 + (x11-mu)^2 + (x12-mu)^2 + (x13-mu)^2 + (x14-mu)^2 + (x15-mu)^2 + (x16-mu)^2 + (x17-mu)^2 + (x18-mu)^2 + (x19-mu)^2 + (x20-mu)^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 re-write `(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)
``````