head(women)
## height weight
## 1 58 115
## 2 59 117
## 3 60 120
## 4 61 123
## 5 62 126
## 6 63 129
dim(women)
## [1] 15 2
str(women)
## 'data.frame': 15 obs. of 2 variables:
## $ height: num 58 59 60 61 62 63 64 65 66 67 ...
## $ weight: num 115 117 120 123 126 129 132 135 139 142 ...
1: The name of the dataset is women.
2: The dimensions function tells us there are 15 rows and 2 columns.
3: There are two variables, height and weight, which are both nums.
4: They are American women of ages 30-39.
summary(women)
## height weight
## Min. :58.0 Min. :115.0
## 1st Qu.:61.5 1st Qu.:124.5
## Median :65.0 Median :135.0
## Mean :65.0 Mean :136.7
## 3rd Qu.:68.5 3rd Qu.:148.0
## Max. :72.0 Max. :164.0
1: Lowest height is 58in and lowest weight is 115lbs
2: Highest height is 72in and highest weight is 164lbs
3: The average height is 65in and the average weight is 136.7lbs
plot(women)
fit = lm(height ~ weight, data = women)
summary(fit)
##
## Call:
## lm(formula = height ~ weight, data = women)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.83233 -0.26249 0.08314 0.34353 0.49790
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 25.723456 1.043746 24.64 2.68e-12 ***
## weight 0.287249 0.007588 37.85 1.09e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.44 on 13 degrees of freedom
## Multiple R-squared: 0.991, Adjusted R-squared: 0.9903
## F-statistic: 1433 on 1 and 13 DF, p-value: 1.091e-14
The p-value indicates that the model is significant.
The R-squared value suggests that it is a useful comparison.
The residual error is quite low.
It seems that a linear model is reasonable for this data due to these factors.
library(ISLR)
library(MASS)
str(Auto)
## 'data.frame': 392 obs. of 9 variables:
## $ mpg : num 18 15 18 16 17 15 14 14 14 15 ...
## $ cylinders : num 8 8 8 8 8 8 8 8 8 8 ...
## $ displacement: num 307 350 318 304 302 429 454 440 455 390 ...
## $ horsepower : num 130 165 150 150 140 198 220 215 225 190 ...
## $ weight : num 3504 3693 3436 3433 3449 ...
## $ acceleration: num 12 11.5 11 12 10.5 10 9 8.5 10 8.5 ...
## $ year : num 70 70 70 70 70 70 70 70 70 70 ...
## $ origin : num 1 1 1 1 1 1 1 1 1 1 ...
## $ name : Factor w/ 304 levels "amc ambassador brougham",..: 49 36 231 14 161 141 54 223 241 2 ...
1: Name of the set is Auto and contains Gas mileage, horsepower, and other information for 392 vehicles.
2: There are 392 rows and 9 columns
3: There are nine variables (mpg, cylinders, displacement, horsepower, weight, acceleration, year, origin, and name), which are mostly nums aside from name which is a factor
head(Auto)
## mpg cylinders displacement horsepower weight acceleration year origin
## 1 18 8 307 130 3504 12.0 70 1
## 2 15 8 350 165 3693 11.5 70 1
## 3 18 8 318 150 3436 11.0 70 1
## 4 16 8 304 150 3433 12.0 70 1
## 5 17 8 302 140 3449 10.5 70 1
## 6 15 8 429 198 4341 10.0 70 1
## name
## 1 chevrolet chevelle malibu
## 2 buick skylark 320
## 3 plymouth satellite
## 4 amc rebel sst
## 5 ford torino
## 6 ford galaxie 500
summary(Auto)
## mpg cylinders displacement horsepower weight
## Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613
## 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225
## Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804
## Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978
## 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615
## Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140
##
## acceleration year origin name
## Min. : 8.00 Min. :70.00 Min. :1.000 amc matador : 5
## 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford pinto : 5
## Median :15.50 Median :76.00 Median :1.000 toyota corolla : 5
## Mean :15.54 Mean :75.98 Mean :1.577 amc gremlin : 4
## 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc hornet : 4
## Max. :24.80 Max. :82.00 Max. :3.000 chevrolet chevette: 4
## (Other) :365
mpg: Min = 9.00, Max = 46.60, Avg = 23.45
cylinders: Min = 3.00, Max = 8.0, Avg = 5.472
displacement: Min = 68.00, Max = 455.60, Avg = 194.4
horsepower: Min = 46.00, Max = 230.00, Avg = 104.5
weight: Min = 1613, Max = 5140, Avg = 2978
acceleration: Min = 8.00, Max = 24.80, Avg = 15.54
year: Min = 70.00, Max = 82.00, Avg = 75.98
origin: Min = 1.00, Max = 3.00 , Avg = 1.577
fit = lm(mpg ~ horsepower, data = Auto)
summary(fit)
##
## Call:
## lm(formula = mpg ~ horsepower, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -13.5710 -3.2592 -0.3435 2.7630 16.9240
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 39.935861 0.717499 55.66 <2e-16 ***
## horsepower -0.157845 0.006446 -24.49 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared: 0.6059, Adjusted R-squared: 0.6049
## F-statistic: 599.7 on 1 and 390 DF, p-value: < 2.2e-16
F-stat: Very low f-statistic shows that the model is significant.
R-squared value: Slightly low, showing the model is maybe not entirely useful and there are other factors that play into the numbers.
Residual error: The error is quite high at 4.906 so it may not fit the model very well.
Q1 and Q3: The residuals were very far apart so the linear model might not be right for the data.
plot(Auto)
fit = lm(year ~ mpg, data = Auto)
summary(fit)
##
## Call:
## lm(formula = year ~ mpg, data = Auto)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.1454 -2.2339 0.1289 2.4404 6.6222
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 69.55545 0.48080 144.67 <2e-16 ***
## mpg 0.27400 0.01946 14.08 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.003 on 390 degrees of freedom
## Multiple R-squared: 0.337, Adjusted R-squared: 0.3353
## F-statistic: 198.3 on 1 and 390 DF, p-value: < 2.2e-16
F-stat: The F-stat is very low so it may be reliable data.
R-squared value: This is very low and indicates a low usefulness of the model.
Residual error: The residual error is slightly high but not as bad as the previous model. Model fits the data ok.
Q1 q3: These residuals are quite far apart and may suggest that the model is not very reasonable for the data.