R: Linear Regression
Code Only
|
Code with Rweb Output
|
|
The following code refers to the Bangladesh Fossil Fuel emission data.
The dataset URL is as follows: http://www.stats4stem.org/uploads/1/7/6/7/1767713/bangledeshh.txt #### CODE STARTS HERE
#### tff = Total Fossil-Fuel Emissions #### gf = Emissions from Gas Fuels plot(tff, gf) # Create a scatterplot # Create a linear model between tff and gf # lm(RESPONSE VARIABLE~EXPLANATORY VARIABLE) or lm(y~x) model=lm(gf~tff) model ### Reports the slope and y-intercept for the model abline(model) ### adds the regression line to the scatterplot ### List outputs that are stored under model that you can call on names(model) ### To call any of the stored names, use ### model$specific.stored.name residuals=model$residuals residuals ### Plot Residuals against x plot(tff,residuals, main="Residual Plot", xlab="Total Fossil Fuel Emissions", ylab="Residuals") ### Add horizontal line at 0 to improve readability of graph abline(h=0) |
> #### CODE STARTS HERE
> #### tff = Total Fossil-Fuel Emissions > #### gf = Emissions from Gas Fuels Rweb > > plot(tff, gf) # Create a scatterplot > # Create a linear model between tff and gf
> # lm(RESPONSE VARIABLE~EXPLANATORY VARIABLE) or lm(y~x) > model=lm(gf~tff) > > model ### Reports the slope and y-intercept for the model Call: lm(formula = gf ~ tff) Coefficients: (Intercept) tff -610.5542 0.6794 > abline(model) ### adds the regression line to the scatterplot > ### List outputs that are stored under model that you can call on > names(model) [1] "coefficients" "residuals" "effects" "rank" [5] "fitted.values" "assign" "qr" "df.residual" [9] "xlevels" "call" "terms" "model" > > ### To call any of the stored names, use > ### model$specific.stored.name > residuals=model$residuals > > residuals 1 2 3 4 5 6 155.39213 78.77021 124.06833 -28.65606 -36.41672 -35.25548 7 8 9 10 11 12 -47.30049 -98.15302 -199.58432 -192.93433 -118.58061 45.67753 13 14 15 16 17 18 139.26874 102.41806 51.82737 30.77548 10.94227 141.92665 19 20 21 22 23 24 -198.59970 129.31028 -55.62168 221.56021 227.14639 -60.51817 25 26 27 28 29 30 -157.39136 -412.97516 65.57052 197.77358 55.66158 -242.23676 31 32 33 34 35 36 -204.90370 -126.43879 18.67426 -70.13089 193.00467 295.92894 > > ### Plot Residuals against x > plot(tff,residuals, + main="Residual Plot", + xlab="Total Fossil Fuel Emissions", + ylab="Residuals") > ### Add horizontal line at 0 to improve readability of graph > abline(h=0) |
Rweb Work Area
DATASET URL:
http://www.stats4stem.org/uploads/1/7/6/7/1767713/bangledeshh.txt
VARIABLES: "year" "tff" "gf" "lf" "sf" "gfl" "cp" "pce.rate" "bf"
http://www.stats4stem.org/uploads/1/7/6/7/1767713/bangledeshh.txt
VARIABLES: "year" "tff" "gf" "lf" "sf" "gfl" "cp" "pce.rate" "bf"




