R Error: Wasn't able to determine range of domain

R Error: Wasn't able to determine range of domain

我正在使用 R 编程语言。

我随机生成了以下数据:

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)

然后我定义了这个函数(正态分布的似然):

  my_function <- function(mu,sigma) {

a = log((1/sqrt(2*pi*sigma^2)) * (exp((x1-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x2-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x3-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x4-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x5-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x6-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x7-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x8-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x9-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x10-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x11-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x12-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x13-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x14-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x15-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x16-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x17-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x18-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x19-mu)/sigma)^2)) +
log((1/sqrt(2*pi*sigma^2)) * (exp((x20-mu)/sigma)^2))
}

但是,当我尝试绘制它时出现问题:

library(plotly)

input_1 <- seq(-100, 100,1)
input_2 <- seq(-100, 100,1)


z <- outer(input_1, input_2, my_function)

plot_ly(x = input_1, y = input_2, z = z) %>% add_surface()

产生以下错误:

Error in scales::col_numeric(pal, rng, na.color = na.color) : 
  Wasn't able to determine range of domain

谁能告诉我为什么会出现这个错误,我该怎么做才能解决这个问题?

所以你的问题在两个方面是病态的:

  • 负西格玛是个问题
  • a zero-valued 西格玛是个问题

所以这是我更新的代码:

my_function <- function(mu,sigma) {

  
  a = log((1/sqrt(2*pi*sigma^2)) * (exp((x1-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x2-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x3-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x4-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x5-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x6-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x7-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x8-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x9-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x10-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x11-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x12-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x13-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x14-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x15-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x16-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x17-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x18-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x19-mu)/sigma)^2)) +
    log((1/sqrt(2*pi*sigma^2)) * (exp((x20-mu)/sigma)^2))
}

library(plotly)

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)

input_1 <- seq(-100, 100,1)
input_2 <- seq(1, 100,1)


z <- outer(input_1, input_2, my_function) 

plot_ly(x = input_1, y = input_2, z = z) %>% add_surface()

它给了我这个情节: