0% found this document useful (0 votes)
61 views5 pages

Code For Week4 - Simple Linear Regression

The document contains R code for analyzing the relationship between carbon footprint and city mpg, as well as income and education levels using linear regression models. It includes plotting residuals, fitted values, and confidence intervals, along with transformations using logarithms for better model fitting. Additionally, it demonstrates time series analysis with consumption and income data, applying linear regression and visualizations.

Uploaded by

Tarun Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views5 pages

Code For Week4 - Simple Linear Regression

The document contains R code for analyzing the relationship between carbon footprint and city mpg, as well as income and education levels using linear regression models. It includes plotting residuals, fitted values, and confidence intervals, along with transformations using logarithms for better model fitting. Additionally, it demonstrates time series analysis with consumption and income data, applying linear regression and visualizations.

Uploaded by

Tarun Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

library(fpp)

library(fpp2)

# Slide 8

plot(jitter(Carbon) ~ jitter(City),xlab="City (mpg)",

ylab="Carbon footprint (tons per year)",data=fuel)

fit <- lm(Carbon ~ City, data=fuel)

abline(fit)

#Slide 9

fit

summary(fit)

#Slide 11

res <- residuals(fit)

plot(jitter(res)~jitter(City), ylab="Residuals", xlab="City", data=fuel)

abline(0,0)

#Slide 15

fitted(fit)[1]

fcast <- forecast(fit, newdata=[Link](City=30))

plot(fcast, xlab="City (mpg)", ylab="Carbon footprint (tons per year)")


# Slide 16

summary(fit)

# Slide 17

confint(fit,level=0.95)

# Slide 19

par(mfrow=c(1,2))

fit2 <- lm(log(Carbon) ~ log(City), data=fuel)

plot(jitter(Carbon) ~ jitter(City), xlab="City (mpg)",

ylab="Carbon footprint (tonnes per year)", data=fuel)

lines(1:50, exp(fit2$coef[1]+fit2$coef[2]*log(1:50)))

plot(log(jitter(Carbon)) ~ log(jitter(City)),

xlab="log City mpg", ylab="log carbon footprint", data=fuel)

abline(fit2)

# Slide 20

res <- residuals(fit2)

plot(jitter(res, amount=0.005) ~ jitter(log(City)),

ylab="Residuals", xlab="log(City)", data=fuel)

#Slide 22

education <- c(10,15,20,25,30,35,40) # x values


income <- c(10^3,15^3,20^3,25^3,30^3,35^3,40^3) #y values

plot(education, income)

fit5<-lm(income ~ education) ## lm is used for linear regression

abline(fit5)

summary(fit5)

res5 <- residuals(fit5)

plot(res5 ~ education)

fit6 <- lm(log(income) ~ log(education))

plot(education,income)

lines(1:50, exp(fit6$coef[1]+fit6$coef[2]*log(1:50)))

plot(log(education),log(income))

abline(fit6)

res6 <- residuals(fit6)

plot(res6 ~ log(education))

# Slide 23

education <- c(12,9,18,10,18,12,18,17,14,8) # x values


income <- c(21000,18000,125000,50000,105000,35000,148000,109000,37000,33000) #y values

plot(education, income)

fit3<-lm(income ~ education) ## lm is used for linear regression

abline(fit3)

summary(fit3)

res3 <- residuals(fit3)

plot(res3 ~ education)

fit4 <- lm(log(income) ~ log(education))

plot(education,income)

lines(1:50, exp(fit4$coef[1]+fit4$coef[2]*log(1:50)))

plot(log(education),log(income))

abline(fit4)

res4 <- residuals(fit4)

plot(res4 ~ log(education))

# SLIDE 24

autoplot(uschange[,c("Consumption","Income")]) +

ylab("% change") + xlab("Year")


# SLIDE 25

uschange %>%

[Link]() %>%

ggplot(aes(x=Income, y=Consumption)) +

ylab("Consumption (quarterly % change)") +

xlab("Income (quarterly % change)") +

geom_point() +

geom_smooth(method="lm", se=FALSE)

# SLIDE 26

tslm(Consumption ~ Income, data=uschange)

summary

#>

#> Call:

#> tslm(formula = Consumption ~ Income, data = uschange)

#>

#> Coefficients:

#> (Intercept) Income

#> 0.545 0.281

You might also like