我正在尝试加速下面的函数(用于以后的引导),该函数执行直线的最小二乘拟合,x 和 y 都有误差。我认为主要的挂断是在while循环中。函数的输入值是观测值x
和y
以及这些值sx
和中的绝对不确定性sy
。
york <- function(x, y, sx, sy){
x <- cbind(x)
y <- cbind(y)
# initial least squares regression estimation
fit <- lm(y ~ x)
a1 <- as.numeric(fit$coefficients[1]) # intercept
b1 <- as.numeric(fit$coefficients[2]) # slope
e1 <- cbind(as.numeric(fit$residuals)) # residuals
theta.fit <- rbind(a1, b1)
# constants
rho.xy <- 0 # correlation between x and y
# initialize york regression
X <- cbind(1, x)
a <- a1
b <- b1
tol <- 1e-15 # tolerance
d <- tol
i = 0
# york regression
while (d > tol || d == tol){
i <- i + 1
a2 <- a
b2 <- b
theta2 <- rbind(a2, b2)
e <- y - X %*% theta2
w <- 1 / sqrt((sy^2) + (b2^2 * sx^2) - (2 * b2 * sx * sy * rho.xy))
W <- diag(w)
theta <- solve(t(X) %*% (W %*% W) %*% X) %*% t(X) %*% (W %*% W) %*% y
a <- theta[1]
b <- theta[2]
mswd <- (t(e) %*% (W%*%W) %*% e)/(length(x) - 2)
sfit <- sqrt(mswd)
Vo <- solve(t(X) %*% (W %*% W) %*% X)
dif <- b - b2
d <- abs(dif)
}
# format results to data.frame
th <- data.frame(a, b)
names(th) <- c("intercept", "slope")
ft <- data.frame(mswd, sfit)
names(ft) <- c("mswd", "sfit")
df <- data.frame(x, y, sx, sy, as.vector(e), diag(W))
names(df) <- c("x", "y", "sx", "sy", "e", "W")
# store output results
list(coefficients = th,
vcov = Vo,
fit = ft,
df = df)
}