Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- > setwd("~/Downloads")
- > # read data from Excel/csv file
- > mail <- read.table("mail(1).csv",header = TRUE,sep=",")
- > # scatter diagram
- >
- > plot(mail$orders, mail$weight, col="red",xlab="order amount",ylab="weight",
- + xlim=range(mail$orders),ylim=range(mail$we .... [TRUNCATED]
- > # using lm to fit linear model, use hardness to predict strength,
- > # tensile strength should be dependent variable
- > mod = lm(formula = weight ~ o .... [TRUNCATED]
- > # using the summary() function to call output of the above model,
- > # output includes coefficients of intercept, slope,residuals, R-squared etc.
- > .... [TRUNCATED]
- Call:
- lm(formula = weight ~ orders, data = mail)
- Residuals:
- Min 1Q Median 3Q Max
- -48.252 -15.772 -2.157 9.047 58.056
- Coefficients:
- Estimate Std. Error t value Pr(>|t|)
- (Intercept) 5.552 15.780 0.352 0.728
- orders 32.760 1.137 28.824 <2e-16 ***
- ---
- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
- Residual standard error: 24.1 on 23 degrees of freedom
- Multiple R-squared: 0.9731, Adjusted R-squared: 0.9719
- F-statistic: 830.8 on 1 and 23 DF, p-value: < 2.2e-16
- > # using least squares method to determine b0 and b1
- > sy <- sd(mail$orders) # sample standard deviation of dependent variable Y
- > sx <- sd(mail$weight)
- > r <- cor(mail$orders,mail$weight) # correlation coefficient between X and Y
- > avgX <- mean(mail$weight)
- > avgY <- mean(mail$orders)
- > # you should obtain the same results given by function "lm" above
- > b1 <- r*sy/sx
- > b0 <- avgY-b1*avgX
- > # retrieve coefficient of determination r-squared
- > summary(mod)$r.squared
- [1] 0.9730622
- > b0 + b1*500
- [1] 15.04256
- > #R squared
- > summary(mod)$r.square
- [1] 0.9730622
- > sse <- sum((fitted(mod) - mean(mail$weight))^2)
- > ssr <- sum((fitted(mod) - mail$weight)^2)
- > 1 - (ssr/(sse + ssr))
- [1] 0.9730622
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement