Guest User

Untitled

a guest
Nov 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. > set.seed(42293888)
  2. > x <- (-4):5
  3. > y <- 2 + x + rnorm(length(x))
  4. > org <- data.frame(x = x, y = y, weights = 1:10)
  5. >
  6. > # show data and fit model. Notice the R-squared
  7. > head(org)
  8. x y weights
  9. 1 -4 0.4963671 1
  10. 2 -3 -0.5675720 2
  11. 3 -2 -0.3615302 3
  12. 4 -1 0.7091697 4
  13. 5 0 0.6485203 5
  14. 6 1 3.8495979 6
  15. > summary(lm(y ~ x, org, weights = weights))
  16.  
  17. Call:
  18. lm(formula = y ~ x, data = org, weights = weights)
  19.  
  20. Weighted Residuals:
  21. Min 1Q Median 3Q Max
  22. -3.1693 -0.4463 0.2017 0.9100 2.9667
  23.  
  24. Coefficients:
  25. Estimate Std. Error t value Pr(>|t|)
  26. (Intercept) 1.7368 0.3514 4.942 0.00113 **
  27. x 0.9016 0.1111 8.113 3.95e-05 ***
  28. ---
  29. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  30.  
  31. Residual standard error: 2.019 on 8 degrees of freedom
  32. Multiple R-squared: 0.8916, Adjusted R-squared: 0.8781
  33. F-statistic: 65.83 on 1 and 8 DF, p-value: 3.946e-05
  34.  
  35. >
  36. > # make redundant data set with redundant rows
  37. > idx <- unlist(mapply(rep, x = 1:nrow(org), times = org$weights))
  38. > org_redundant <- org[idx, ]
  39. > head(org_redundant)
  40. x y weights
  41. 1 -4 0.4963671 1
  42. 2 -3 -0.5675720 2
  43. 2.1 -3 -0.5675720 2
  44. 3 -2 -0.3615302 3
  45. 3.1 -2 -0.3615302 3
  46. 3.2 -2 -0.3615302 3
  47. >
  48. > # fit model and notice the same R-squared
  49. > summary(lm(y ~ x, org_redundant))
  50.  
  51. Call:
  52. lm(formula = y ~ x, data = org_redundant)
  53.  
  54. Residuals:
  55. Min 1Q Median 3Q Max
  56. -1.19789 -0.29506 -0.05435 0.33131 2.36610
  57.  
  58. Coefficients:
  59. Estimate Std. Error t value Pr(>|t|)
  60. (Intercept) 1.73680 0.13653 12.72 <2e-16 ***
  61. x 0.90163 0.04318 20.88 <2e-16 ***
  62. ---
  63. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  64.  
  65. Residual standard error: 0.7843 on 53 degrees of freedom
  66. Multiple R-squared: 0.8916, Adjusted R-squared: 0.8896
  67. F-statistic: 436.1 on 1 and 53 DF, p-value: < 2.2e-16
  68.  
  69. >
  70. > # glm gives you the same with family = gaussian()
  71. > # just compute the R^2 from the deviances. See
  72. > # https://stats.stackexchange.com/a/46358/81865
  73. > fit <- glm(y ~ x, family = gaussian(), org_redundant)
  74. > fit$coefficients
  75. (Intercept) x
  76. 1.7368017 0.9016347
  77. > 1 - fit$deviance / fit$null.deviance
  78. [1] 0.8916387
Add Comment
Please, Sign In to add comment