<repo_name>rahulmnair1997/MATH564-IIT<file_sep>/home_work=4/homework_4_question_2.R
# @author - rahul_nair
setwd("/Users/rahulnair/desktop")
data_1 <- read.csv('cement.csv', header = TRUE)
y <- data_1$y
x1 <- data_1$x1
x2 <- data_1$x2
x3 <- data_1$x3
x4 <- data_1$x4
mod <- lm(y~x1+x2+x3+x4)

# intercept only model
model <- lm(y~1)


add1(model, ~.+x1+x2+x3+x4, test = "F")

# adding x4
model <- update(model, .~.+x4)
summary(model)

add1(model, ~.+x1+x2+x3, test = "F")

# adding x1
model <- update(model,.~.+x1)
summary(model)

add1(model, ~.+x2+x3, test = "F")

# adding x2
model <- update(model, .~.+x2)
summary(model)

# removing x4
model <- update(model, .~.-x4)
summary(model)
add1(model, ~.+x3+x4, test = "F")
# Thus our final model will have x1 and x2.<file_sep>/home_work=2/homework_2_i.R
DST=read.table("APPENC07.txt",fill = FALSE, header = TRUE) 
X=DST$sqft
Y=DST$Price
z=sample(1:522,100)
x=X[z]
y=Y[z]
fit=lm(formula = y~x)
plot(x,y,main="Sales Price v/s Finished Square Feet")
abline(fit,col="red")
conf_interval = predict(fit,newdata = data.frame(y), interval="confidence",
                        level = 0.95)
lines(x, conf_interval[,2], col="blue", lty=2)
lines(x, conf_interval[,3], col="blue", lty=2)
pred_interval = predict(fit,newdata = data.frame(y), interval="prediction",
                        level = 0.95)
lines(x, pred_interval[,2], col="orange", lty=2)
lines(x, pred_interval[,3], col="orange", lty=2)
legend(1000,8e+05, legend=c("Regression", "95% CI", "95% PI"),
       col=c("red", "blue", "orange"), lty=1:2, cex=0.8)
x2=sum((x-mean(x))^2)
y2=sum((x-mean(x))*(y-mean(y)))
b1=y2/x2
b0=mean(y)-b1*mean(x)
err=y-b0-b1*x
sse=sum(err^2)
mse=sse/98
t=qt(0.975,98)
se_conf=sqrt(mse/100)
y_hat=b0+b1*mean(x)
y_confUpper=y_hat+t*se_conf
y_confLower=y_hat-t*se_conf
se_pred=sqrt(mse*(1+1/100))
y_predUpper=y_hat+t*se_pred
y_predLower=y_hat-t*se_pred
conf_interval_y <- predict(fit, newdata=data.frame(y), interval="confidence",level = 0.95)
pred_interval_y <- predict(fit, newdata=data.frame(y), interval="prediction",level = 0.95)
y_confUpper1=mean(conf_interval_y[,3])
y_confLower1=mean(conf_interval_y[,2])
y_predUpper1=mean(pred_interval_y[,3])
y_predLower1=mean(pred_interval_y[,2])<file_sep>/home_work=1/home_work_1_question_4.R
x <- runif(40,-1,1)
x
y <- 2*x + rnorm(40,0,0.1)
y
# For regression passing through origin
# Summation of x^2
sum_x_sqr <- sum((x)^2)
# Summation of x*y
sum_xy <- sum((x)*(y))
# Calculating b0
b0 <- 0
# Calculating b1
b1 <- sum_xy/sum_x_sqr
# Calculating e_2
e_1 <- y - b1*x
sum_of_e1 = sum(e_1)
sum_of_e1
# Calculating r^2
sst <- sum((y - mean(y))^2)
sse <- sum((e_1)^2)
r_1 <- 1 - (sse/sst)
r_1
# For ordinary linear regression
# Summation of x^2
sum_x_sqr <- sum((x - mean(x))^2)
# Summation of x*y
sum_xy <- sum((x - mean(x))*(y - mean(y)))
# Calculating b1
b1 <- sum_xy/sum_x_sqr
# Calculating b0
b0 <- (mean(y)) - (b1*mean(x))
# Calcualting e_2
e_2 <- y - b0 - b1*x
sum_of_e2 = sum(e_2)
sum_of_e2
# Calculating r^2
sst <- sum((y - mean(y))^2)
sse <- sum((e_2)^2)
r_2 <- 1 - (sse/sst)
r_2
<file_sep>/home_work=3/homework_3.R
setwd("/Users/rahulnair/desktop")
#read data
data_1 <- read.table('Copier.txt', header = FALSE)
data_2 <- read.table('Model.txt', header = FALSE)
x1 <- data_1$V2
x1
x2 <- data_2$V1
x2
y <- data_1$V1

# (1)
RModel <- lm(y~x1+x2)
summary(RModel)

# (2)
confint(RModel, level=0.95)

# (3)
x <- x1*x2
#plot(x, resid(RModel))

# (4)
RModel1 <- lm(y~x1+x2+x)
summary(RModel1)
#plot(x, resid(RModel1))
#abline(RModel, col = 'red')
#abline(RModel1, col = 'blue')
#plot(RModel)
coplot(y~x1|x2, panel = panel.smooth)

# (5)
anova(RModel1)<file_sep>/home_work=1/home_work_1_question_5.R
#library("data.table")
setwd("/Users/rahulnair/desktop")
data <- read.delim('skin cancer2.txt', header = TRUE, sep = "|")
data$Lat
x <- data$Lat
y <- data$Mort
# Calculating the estimates
sum_x_sqr = sum((x - mean(x))^2)
sum_xy = sum((y - mean(y)) * (x - mean(x)))
# Calculating b1
b1 <- sum_xy/sum_x_sqr
b0 <- (mean(y)) - (b1*(mean(x)))
b1
# Calculating e
e <- y - b0 - (b1*x)
sum(e)
# Calculating r
sst <- sum((y - mean(y))^2)
sst
sse <- sum((e^2))
sse
ssr <- sum(((b0+b1*x) - mean(y))^2)
ssr
# confidence interval
b1_lower <- b1 - ((qt((0.05)/2,(46)))*abs(sqrt(sse/46)/sqrt(sum((x-mean(x))^2))))
b1_lower
b1_upper <- b1+  ((qt((0.05)/2,(46)))*abs(sqrt(sse/46)/sqrt(sum((x-mean(x))^2))))
b1_upper
# with functions
reg<-lm(y~x)
summary(reg)
confint(reg,level=0.95)
plot(x,y,pch=16,cex=1.3, col="blue", main="Mortality vs Latitude", xlab="Latitude", ylab="Mortality")
abline(lm(y~x))
<file_sep>/home_work=4/homework_4_question_1.R
# @author - rahul_nair
# QUESTION 1
setwd("/Users/rahulnair/desktop")
data <- read.csv('CDI_1.csv', header = TRUE)
x1 <- data$variable_6
x2 <- data$variable_8
x3 <- data$variable_9
x4 <- data$variable_13
x5 <- data$variable_14
x6 <- data$variable_15
y <- data$variable_10
# data_2 <- data[,c('variable_6','variable_8','variable_9','variable_13','variable_14','variable_15')]
# PART 1
# install.packages("car")
library("car")
vif(lm(y~x1+x2+x3+x4+x5+x6), data=Duncan)
# 2 variables, x2 and x3 have serious multi-collinearity issues at present.

########
# PART 2
#install.packages("MASS")
library("MASS")
deleted_residuals <-rstudent(lm(y~x1+x2+x3+x4+x5+x6))
mod <- lm(y~x1+x2+x3+x4+x5+x6)
ols_plot_resid_stud_fit(mod)
# a graph has been plotted. There are 9 outliers.

########
# PART 3
diag = hatvalues(mod, type = "diagonal")
diag
extreme <- c()
for (i in 1:length(diag)){
  if(diag[i]>3*mean(diag)){
    extreme <- c(extreme, diag[i])
  }
}
extreme

#######
# PART 4(I)
# We check the threshold for dfBetas (by ols_plot_dfbetas(mod), which is 0.1 and based on that we get influence values  which is 2, 6.
print("dfbetas")
dfbetas(mod)[c(2, 6, 8, 48, 128, 206, 404),]
ols_plot_dfbetas(mod)


########
#PART 4(II)
# We check the threshold for dffits (by ols_plot_dffits(mod), which is 0.25 and based on that we get influence values  which is 6. 
print("dffits")
dffits(mod)[c(2, 6, 8, 48, 128, 206, 404)]
ols_plot_dffits(mod)

########
#PART 4(III)
# We know the threshold which is p = 0.5 and based on that we get influence values  which is 6. 
print("cook's distance")
cook = cooks.distance(mod)[c(2,6,  8, 48, 128, 206, 404)]
cook
pf(cook,7,433)

