-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9c3156
commit b270897
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#Install the 'Metrics' package if you do not have that installed | ||
|
||
#Loading the required libraries | ||
library(Metrics) | ||
|
||
#Reading the dataset | ||
s_data<-read.csv('http://bit.ly/w-data',header=T) | ||
|
||
#Dimensions of the dataset | ||
dim(s_data) | ||
|
||
#A glimpse of the dataset | ||
head(s_data,n=5) | ||
|
||
#Column names of the dataset | ||
names(s_data) | ||
|
||
#Plotting the dataset to get a clear view of the dataset | ||
par(bg='#CCFFFF') | ||
plot(s_data$Hours,s_data$Scores,pch=16,col='dark blue', | ||
xlab='Hours studied',ylab='Precentage Score', | ||
main='Hours vs. Percentage') | ||
legend(x=1,y=95,'Scores',pch=16,col='dark blue') | ||
|
||
#Seperate the train and test data | ||
X<-s_data$Hours;Y<-s_data$Scores | ||
t_sample<-sample(nrow(s_data),floor(0.8*nrow(s_data)),replace=F) | ||
X_train<-X[t_sample];Y_train<-Y[t_sample] | ||
X_test<-X[-t_sample];Y_test<-Y[-t_sample] | ||
|
||
#Fit a linear model | ||
model<-lm(Y_train~X_train) | ||
par(bg='#CCFFCC') | ||
plot(s_data$Hours,s_data$Scores,pch=16,col='dark blue', | ||
xlab='Hours studied',ylab='Precentage Score', | ||
main='Hours vs. Percentage') | ||
abline(model,col='red',lwd=2) | ||
|
||
#Predictions | ||
X_test#Testing data | ||
Y_predicted<-predict(model,newdata=data.frame(X_train=X_test))#Predicting the scores | ||
df<-data.frame('Hours'=X_test,'Actual score'=Y_test,'Predicted score'=Y_predicted) | ||
df | ||
pred<-predict(model,newdata=data.frame(X_train=9.25)) | ||
pr_data<-data.frame('Hours'=9.25,'Predicted Score'=pred) | ||
pr_data | ||
|
||
#Accuracy | ||
mae(df$Actual,df$Predicted) |