Exp 2 145

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

EXPERIMENT 2

ROLL NO-21BME145
1) Prepare R code for preforming linear regression prediction of a given
dataset using gradient descent as optimization technique for minimizing
the cost function. Consider single dependent and single independent
variable for the same.
CODE

x=seq(1,100,length.out=1000)
w0=1
w1=2
set.seed(100)
noise=rnorm(length(x), mean=0, sd=1)
y=3*x+4+noise
err0=1
err1=1
erJ=1
m=length(x)
while(abs(erJ)>0.000000000001)
{
J0=(1/(2*m))*sum((y-w0-(w1*x))^2)
df1=(-1/m)*(sum(y-w0-(w1*x)))
df2=(-1/m)*(sum((x*y)-(w0*x)-(w1*x*x)))
w0=w0-(0.0001*df1)
w1=w1-(0.0001*df2)
J1=(1/(2*m))*sum((y-w0-(w1*x))^2)
erJ=J0-J1
}
2) A database containing compressive strength of concrete is shared with
you on LMS platform. Build a linear regression model to predict the
compressive strength of the concrete with respect to other given
parameter. Cross validate the model with 10 fold cross validation
strategy. Estimate the average MSE for cross validation.

CODE
data=read.csv('Concrete_Data.csv')
library(caret)
library(caTools)
predictors = data[,-9]
Compressive_Strength = data$csMPa
set.seed(123)
train_MSE=rep(NA,10)
test_MSE=rep(NA,10)
for(i in 1:10)
{
split=sample.split(data$csMPa,SplitRatio=0.70)
train=subset(data,split==TRUE)
test=subset(data,split==FALSE)
#training the model with train data set
model=lm(train$csMPa~.,data=train)
summary(model)
#training accuracy
predict_train=predict(model,train[1:8])
plot(train$csMPa,t='l',col='black', ylab='Compressive_Strength')
lines(predict_train,t='l',col='red', ylab='Compressive_Strength')
predict_test=predict(model,test[1:8])
test_MSE[i]=(sum(test$csMPa-predict_test)^2)/length(predict_test)
train_MSE[i]=(sum(train$csMPa-predict_train)^2)/length(predict_train)
}

average_mse = mean(train_MSE)

You might also like