Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. > setwd("~/Downloads")
  2.  
  3. > # read data from Excel/csv file
  4. > mail <- read.table("mail(1).csv",header = TRUE,sep=",")
  5.  
  6. > # scatter diagram
  7. >
  8. > plot(mail$orders, mail$weight, col="red",xlab="order amount",ylab="weight",
  9. + xlim=range(mail$orders),ylim=range(mail$we .... [TRUNCATED]
  10.  
  11. > # using lm to fit linear model, use hardness to predict strength,
  12. > # tensile strength should be dependent variable
  13. > mod = lm(formula = weight ~ o .... [TRUNCATED]
  14.  
  15. > # using the summary() function to call output of the above model,
  16. > # output includes coefficients of intercept, slope,residuals, R-squared etc.
  17. > .... [TRUNCATED]
  18.  
  19. Call:
  20. lm(formula = weight ~ orders, data = mail)
  21.  
  22. Residuals:
  23. Min 1Q Median 3Q Max
  24. -48.252 -15.772 -2.157 9.047 58.056
  25.  
  26. Coefficients:
  27. Estimate Std. Error t value Pr(>|t|)
  28. (Intercept) 5.552 15.780 0.352 0.728
  29. orders 32.760 1.137 28.824 <2e-16 ***
  30. ---
  31. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
  32.  
  33. Residual standard error: 24.1 on 23 degrees of freedom
  34. Multiple R-squared: 0.9731, Adjusted R-squared: 0.9719
  35. F-statistic: 830.8 on 1 and 23 DF, p-value: < 2.2e-16
  36.  
  37.  
  38. > # using least squares method to determine b0 and b1
  39. > sy <- sd(mail$orders) # sample standard deviation of dependent variable Y
  40.  
  41. > sx <- sd(mail$weight)
  42.  
  43. > r <- cor(mail$orders,mail$weight) # correlation coefficient between X and Y
  44.  
  45. > avgX <- mean(mail$weight)
  46.  
  47. > avgY <- mean(mail$orders)
  48.  
  49. > # you should obtain the same results given by function "lm" above
  50. > b1 <- r*sy/sx
  51.  
  52. > b0 <- avgY-b1*avgX
  53.  
  54. > # retrieve coefficient of determination r-squared
  55. > summary(mod)$r.squared
  56. [1] 0.9730622
  57.  
  58. > b0 + b1*500
  59. [1] 15.04256
  60.  
  61. > #R squared
  62. > summary(mod)$r.square
  63. [1] 0.9730622
  64.  
  65. > sse <- sum((fitted(mod) - mean(mail$weight))^2)
  66.  
  67. > ssr <- sum((fitted(mod) - mail$weight)^2)
  68.  
  69. > 1 - (ssr/(sse + ssr))
  70. [1] 0.9730622
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement