-1

Using brute force method, I have to find the maximum likelihood estimates where: we have to vary µ from 5 to 15 and σ from 0.5 to 1.5, both in increments of 0.1 This is what I have so far:

data = 8.453532, 10.025041,11.495339, 9.367600, 8.333229, 9.788753, 10.883344, 10.543059, 9.869095, 10.799819
mu = seq(5, 15, 0.1)
sigma = seq(0.5, 1.5, 0.1)
neglog <- array(dim=c(mu), dim=c(sigma))

Any idea how to continue the code?

Rasha
  • 1
  • 1

1 Answers1

0

You can list all possible unique combinations of two vectors by:

res <- expand.grid(mu, sigma)
names(res) <- c("mu", "sigma")

res
   mu sigma
1 5.0   0.5
2 5.1   0.5
3 5.2   0.5
4 5.3   0.5
5 5.4   0.5
6 5.5   0.5
...     ...
1107 14.6   1.5
1108 14.7   1.5
1109 14.8   1.5
1110 14.9   1.5
1111 15.0   1.5

Probably one can use expand.grid to find all combination of the indexes:

indexes <- expand.grid(seq(mu), seq(sigma))
names(indexes) <- c("mu", "sigma")
# use the indexes to get the actual values
indexes$mu <- mu[indexes$mu]
indexes$sigma <- sigma[indexes$sigma]
# call the data frame as `res`ult
res <- indexes

# now `res` contains all the combinations of the values of `mu` and `sigma`.

But you could use a normal nested for loop

res <- array(dim=c(length(mu) * length(sigma), 2))
idx <- 0
for (i in seq(mu)) {
  for (j in seq(sigma)) {
    idx <- idx + 1
    res[idx,] <- c(mu[i], sigma[j])
  }
}
# then `res` contains all combinations!
Gwang-Jin Kim
  • 9,303
  • 17
  • 30