# Uses the function varbeta0hat to calculate the variance of the y-intercept in a # straight line regression with 10 values of x. # #Set up the constraints on the x-values diag10 <- diag(1,10) cmat <- rbind(diag10,-diag10) uvec <- rep(c(0,-1),each=10) varbeta0hat <- function(x) { a <- sum(x^2) v <- a/(10*a - (sum(x))^2) v } #Then the R function constrOptim is used to minimise varbeta0hat. start <- c(0.0026,0.0076,0.0088,0.0266,0.0450,0.0523,0.0564,0.0631, 0.1301,0.9665) out <- constrOptim(start,varbeta0hat,NULL,cmat,uvec,method="Nelder-Mead") out # The function gradvar gives the partial derivatives of the variance with repect to each of each x-value. gradvar <- function(x) { a <- sum(x^2) b <- sum(x) num <- 2*b*(a - b*x) denom <- (10*a - b^2)^2 gradvector <- num/denom gradvector } #Now use constrOptim with gradvar to find the optimum solution start <- c(0.0026,0.0076,0.0088,0.0266,0.0450,0.0523,0.0564,0.0631, 0.1301,0.9665) out <- constrOptim(start,varbeta0hat,gradvar,cmat,uvec) out