################################################################### # 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 1: Regression through the origin. cat("Model 1: Regression Though the Origin\n") # Set up X matrix and y vector. X = matrix(0:4, 5, 1, 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) - 1 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 1: Regression Though the Origin X matrix: [,1] [1,] 0 [2,] 1 [3,] 2 [4,] 3 [5,] 4 y vector: [,1] [1,] 0 [2,] 49 [3,] 101 [4,] 149 [5,] 201 beta vector: [,1] [1,] 50.06667 yhat vector: [,1] [1,] 0.00000 [2,] 50.06667 [3,] 100.13333 [4,] 150.20000 [5,] 200.26667 residual vector: [,1] [1,] 0.0000000 [2,] -1.0666667 [3,] 0.8666667 [4,] -1.2000000 [5,] 0.7333333 Sum of squares for error (SSE): Degrees of freedom for error (DFE): Mean of squares for error (MSE): Covariance matrix for beta. [,1] [1,] 0.03222222 Standard error of parameter estimate. [1] 0.1795055 > source(spring.r) Model 1: Regression Though the Origin X matrix: [,1] [1,] 0 [2,] 1 [3,] 2 [4,] 3 [5,] 4 y vector: [,1] [1,] 0 [2,] 49 [3,] 101 [4,] 149 [5,] 201 beta vector: [,1] [1,] 50.06667 yhat vector: [,1] [1,] 0.00000 [2,] 50.06667 [3,] 100.13333 [4,] 150.20000 [5,] 200.26667 residual vector: [,1] [1,] 0.0000000 [2,] -1.0666667 [3,] 0.8666667 [4,] -1.2000000 [5,] 0.7333333 Sum of squares for error (SSE): [1] 3.866667 Degrees of freedom for error (DFE): [1] 4 Mean of squares for error (MSE): [1] 0.9666667 Covariance matrix for beta. [,1] [1,] 0.03222222 Standard error of parameter estimate. [1] 0.1795055