################################################################### # SpringMatrix Example -- Source code file spring-matrix-model1.r # # # # Matrix calculations of regression model parameter estimates # # and standard errors for SpringReg Example using R # ################################################################### # Model 2: Simple Linear Regression. cat(Model 2: Simple Linear Regression\n) # Set up X matrix and y vector. X = matrix(c(1, 0, 1, 1, 1, 2, 1, 3, 1, 4), 5, 2, byrow=T) y = matrix(c(0,49,101,149,201), 5, 1, byrow=T) cat("X matrix:\n") print(X) cat("y vector:\n") print(y) # Calculate estimated regression parameters. beta = solve(t(X) %*% X) %*% t(X) %*% y cat("beta vector:\n") print(beta) # Find the predicted value vector. yhat = X %*% beta cat("yhat vector:\n") print(yhat) # Find the residuals. resids = y - yhat cat("residual vector:\n") print(resids) # Compute SSE. cat("Sum of squares for error (SSE):\n") sse = t(resids) %*% resids print(sse) # Compute DFE cat("Degrees of freedom for error (DFE):\n") dfe = length(y) - 2 print(dfe) # Compute MSE cat("Mean of squares for error (MSE):\n") mse = sse / dfe print(mse) # Calculate covariance matrix. # Get MSE from SAS or R output. covbeta = solve(t(X) %*% X) * as.numeric(mse) cat("Covariance matrix for beta.\n") print(covbeta) # Calculate standard error of parameter estimate. sdbeta = sqrt(diag(covbeta)) cat("Standard error of parameter estimate.\n") print(sdbeta) ############################################### # Output: # ############################################### Model 2: Simple Linear Regression X matrix: [,1] [,2] [1,] 1 0 [2,] 1 1 [3,] 1 2 [4,] 1 3 [5,] 1 4 y vector: [,1] [1,] 0 [2,] 49 [3,] 101 [4,] 149 [5,] 201 beta vector: [,1] [1,] -0.4 [2,] 50.2 yhat vector: [,1] [1,] -0.4 [2,] 49.8 [3,] 100.0 [4,] 150.2 [5,] 200.4 residual vector: [,1] [1,] 0.4 [2,] -0.8 [3,] 1.0 [4,] -1.2 [5,] 0.6 Sum of squares for error (SSE): [1] 3.6 Degrees of freedom for error (DFE): [1] 3 Mean of squares for error (MSE): [1] 1.2 Covariance matrix for beta. [,1] [,2] [1,] 0.72 -0.24 [2,] -0.24 0.12 Standard error of parameter estimate. [1] 0.8485281 0.3464102