Assignment - 3.R: 2021-08-02 by Harshith H S 2001115

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Assignment_3.

R
2021-08-02
By
Harshith H S
2001115

##Part I
fit<-lm(weight~height, data=women)

summary(fit)

##
## Call:
## lm(formula = weight ~ height, data = women)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.7333 -1.1333 -0.3833 0.7417 3.1167
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -87.51667 5.93694 -14.74 1.71e-09 ***
## height 3.45000 0.09114 37.85 1.09e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.525 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

coefficients(fit)

## (Intercept) height
## -87.51667 3.45000

fitted(fit)

## 1 2 3 4 5 6 7 8
## 112.5833 116.0333 119.4833 122.9333 126.3833 129.8333 133.2833 136.7333
## 9 10 11 12 13 14 15
## 140.1833 143.6333 147.0833 150.5333 153.9833 157.4333 160.8833

residuals(fit)
## 1 2 3 4 5 6
## 2.41666667 0.96666667 0.51666667 0.06666667 -0.38333333 -0.83333333
## 7 8 9 10 11 12
## -1.28333333 -1.73333333 -1.18333333 -1.63333333 -1.08333333 -0.53333333
## 13 14 15
## 0.01666667 1.56666667 3.11666667

confint(fit)

## 2.5 % 97.5 %
## (Intercept) -100.342655 -74.690679
## height 3.253112 3.646888

anova(fit)

## Analysis of Variance Table


##
## Response: weight
## Df Sum Sq Mean Sq F value Pr(>F)
## height 1 3332.7 3332.7 1433 1.091e-14 ***
## Residuals 13 30.2 2.3
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

predict(fit)

## 1 2 3 4 5 6 7 8
## 112.5833 116.0333 119.4833 122.9333 126.3833 129.8333 133.2833 136.7333
## 9 10 11 12 13 14 15
## 140.1833 143.6333 147.0833 150.5333 153.9833 157.4333 160.8833

plot(women$height,women$weight,xlab="Height(in inches)",ylab="Weight (in


pounds)")
abline(fit)

## Interpretation of Part I:

##Since the p value is almost zero (ANOVA), it is significant, hence


we reject null hypothesis, and conclude that there is significant
dependency of dependent variable(weight) on the independent
variable(height)

## R-squared value is in excess of 0.99, which indicates that the


regression term explains >99% of the variability in the dependent
variable

## Since the p value is significant for both “intercept” and “height”,


we conclude that weight is significantly dependent on both these
factors (Coefficients table)
##For every inch of increase in height, there is an increase of 3.45
lbs (within the aforementioned range of weights)

##Part II - Examining Bivariate relationships


states<-
as.data.frame(state.x77[,c("Murder","Population","Illiteracy","Income","Frost
")])
cor(states)

## Murder Population Illiteracy Income Frost


## Murder 1.0000000 0.3436428 0.7029752 -0.2300776 -0.5388834
## Population 0.3436428 1.0000000 0.1076224 0.2082276 -0.3321525
## Illiteracy 0.7029752 0.1076224 1.0000000 -0.4370752 -0.6719470
## Income -0.2300776 0.2082276 -0.4370752 1.0000000 0.2262822
## Frost -0.5388834 -0.3321525 -0.6719470 0.2262822 1.0000000

library(car)

## Loading required package: carData

scatterplotMatrix(states,spread=FALSE,
smoother.args=list(lty=2),main="Scatter Plot Matrix")

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter


## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter


## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is
not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter


## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter


## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical
parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter
## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter
## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter


## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter

## Warning in plot.window(...): "spread" is not a graphical parameter

## Warning in plot.window(...): "smoother.args" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "spread" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "smoother.args" is not a graphical


parameter

## Warning in title(...): "spread" is not a graphical parameter

## Warning in title(...): "smoother.args" is not a graphical parameter


## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is
not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...): "spread" is


not a
## graphical parameter

## Warning in axis(side = side, at = at, labels = labels, ...):


"smoother.args" is
## not a graphical parameter

#Multiple linear regression


states<-
as.data.frame(state.x77[,c("Murder","Population","Illiteracy","Income","Frost
")])
fit<-lm(Murder~Population + Illiteracy + Income + Frost,data=states)
summary(fit)

##
## Call:
## lm(formula = Murder ~ Population + Illiteracy + Income + Frost,
## data = states)
##
## Residuals:
## Min 1Q Median 3Q Max
## -4.7960 -1.6495 -0.0811 1.4815 7.6210
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.235e+00 3.866e+00 0.319 0.7510
## Population 2.237e-04 9.052e-05 2.471 0.0173 *
## Illiteracy 4.143e+00 8.744e-01 4.738 2.19e-05 ***
## Income 6.442e-05 6.837e-04 0.094 0.9253
## Frost 5.813e-04 1.005e-02 0.058 0.9541
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 2.535 on 45 degrees of freedom
## Multiple R-squared: 0.567, Adjusted R-squared: 0.5285
## F-statistic: 14.73 on 4 and 45 DF, p-value: 9.133e-08

## Interpretation of Part II:

## R-squared value is near 0.5, hence these factors (population,


illiteracy, income and frost) explain only about half of the
variability in the murders, and rest is attributed to the error term

## The only significant independent variables are population(at 95%


level of confidence) and illiteracy (at 99.9% level of confidence)

You might also like