Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. n <- 72 # Number of observations
  2. k <- 37 # Number of variables, apart from the constant
  3. beta <- (-1)^seq_len(k+1) # The model coefficients, beginning with the intercept
  4. sigma <- 0.5 # Error s.d.
  5. #
  6. # Create data.
  7. #
  8. X <- as.data.frame(matrix(rnorm(n*k), nrow=n, dimnames=list(NULL, paste0("x.", 1:k))))
  9. X$y <- cbind(1, as.matrix(X)) %*% beta + rnorm(n, 0, sigma) # The response
  10. #
  11. # Base model.
  12. #
  13. fit <- lm(y ~ ., X)
  14. summary(fit)
  15. #
  16. # Paired model.
  17. #
  18. xx <- as.matrix(X)
  19. XX <- as.data.frame(t(apply(combn(1:n, 2), 2, function(ij) xx[ij[1], ] - xx[ij[2], ])))
  20. names(XX) <- paste0("d", names(XX))
  21. fit.d <- lm(dy ~ . - 1, XX) # No intercept!
  22. summary(fit.d)
  23. #
  24. # Check whether coefficients agree.
  25. #
  26. all.equal(coefficients(fit)[-1], coefficients(fit.d), check.attributes=FALSE)
  27. #
  28. # Plot the data.
  29. #
  30. # pairs(X)
  31. # pairs(XX)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement