【发布时间】:2022-01-19 20:03:39
【问题描述】:
x <- c(-3,-2.5,-2,-1.5,-1,-0.5)
y <- c(2,2.5,2.6,2.9,3.2,3.3)
挑战在于整个数据都是从左斜率开始的,如何生成双边高斯分布?
【问题讨论】:
-
问题,你想让分布的平均值变化还是固定在0?
x <- c(-3,-2.5,-2,-1.5,-1,-0.5)
y <- c(2,2.5,2.6,2.9,3.2,3.3)
挑战在于整个数据都是从左斜率开始的,如何生成双边高斯分布?
【问题讨论】:
【讨论】:
没有办法用这些密度拟合高斯分布。如果提供了正确的 y 值,这将是解决问题的一种方法:
# Define function to be optimized
f <- function(pars, x, y){
mu <- pars[1]
sigma <- pars[2]
y_hat <- dnorm(x, mu, sigma)
se <- (y - y_hat)^2
sum(se)
}
# Define the data
x <- c(-3,-2.5,-2,-1.5,-1,-0.5)
y <- c(2,2.5,2.6,2.9,3.2,3.3)
# Find the best paramters
opt <- optim(c(-.5, .1), f, 'SANN', x = x, y = y)
plot(
seq(-5, 5, length.out = 200),
dnorm(seq(-5, 5, length.out = 200), opt$par[1], opt$par[2]), type = 'l', col = 'red'
)
points(c(-3,-2.5,-2,-1.5,-1,-0.5), c(2,2.5,2.6,2.9,3.2,3.3))
【讨论】:
使用 nls 获得 y 与 .lin.a * dnorm(x, b, c) 的最小二乘拟合,其中 .lin.a、b 和 c 是要估计的参数。
fm <- nls(y ~ cbind(a = dnorm(x, b, c)),
start = list(b = mean(x), c = sd(x)), algorithm = "plinear")
fm
给予:
Nonlinear regression model
model: y ~ cbind(a = dnorm(x, b, c))
data: parent.frame()
b c .lin.a
0.2629 3.2513 27.7287
residual sum-of-squares: 0.02822
Number of iterations to convergence: 7
Achieved convergence tolerance: 2.582e-07
dnorm 模型(黑色曲线)似乎适合这些点,尽管即使是只涉及两个参数(截距和斜率)而不是 3 的直线(蓝线)也不错。
plot(y ~ x)
lines(fitted(fm) ~ x)
fm.lin <- lm(y ~ x)
abline(fm.lin, col = "blue")
【讨论】: