Q:

Minimize sum of squares function, then extract variance-covariance

In brief my problem concentrated in extracting variance-covariance matrix from minimized sum of squares function. Below is a simple example of what I try to minimize.

Given:
p <- c(7.8, 4.1, 6.2, 5.3, 10.1, 9.6)
x <- 1:6
f <- function(x,q){x^2-q*x+14}

sum of squared errors of prediction
SSE <- function(q){ sum((p-f(x,q))^2) }

Minimizing example then calculating variance covariance matrix is the main point.
Any help would be appreciated.

A:

Since your model is linear you can use lm:
p <- c(7.8, 4.1, 6.2, 5.3, 10.1, 9.6)
x <- 1:6
f <- function(x,q){x^2-q*x+14}

fit <- lm(I(p - x^2 - 14) ~ x + 0)
summary(fit)$coef
#   Estimate Std. Error   t value     Pr(>|t|)
#x -6.275824   0.212917 -29.47545 8.426765e-07
vcov(fit)
#           x
#x 0.04533366

#illustrate that this is the minimum
f <- function(x,q){x^2-q*x+14}
SSE <- function(q){ sum((p-f(x,q))^2) }

a <- seq(from = 0, to =10, by = 0.001)
plot(a, sapply(a, SSE), type = "l")
abline(v = -coef(fit))

