-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.R
62 lines (44 loc) · 1.73 KB
/
script.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
###------- Starting a machine learning project ---------###
# Packages
library("tidyverse")
library(rpart) # for regression trees
library(randomForest) # for random forests
library("modelr") #Measuring model accuracy
#Read the data from a table and visualize
data<-read.csv(".../train.csv")
# Overview
summary(data)
names(data)
nrow(data)
ncol(data)
###------- ( Regression Tree )---------###
fit<- rpart(SalePrice~LotArea+YearBuilt, data=data)
plot(fit, uniform=TRUE)
text(fit, cex=.6)
#Predictions based on model
print("Predictions for the following 5 houses")
print(head(data))
print("The predictions are")
print(predict(fit, head(data)))
print("Actual price")
print(head(data$SalePrice))
#Getting the mean average error (MAE). On average, our predictions are off by about X.
mae(model=fit, data=data)
# Separating data into testing an training data.
splitdata<-resample_partition(data, c(test=0.3, train=0.7))
# How many cases are in test and training set?
lapply(splitdata, dim)
# Fitting a new model to our trainig data
fit2<- rpart(SalePrice~LotArea+YearBuilt, data=splitdata$train)
mae(model=fit2, data=splitdata$test)
###------- ( Random forest )---------###
fitRandomForest <- randomForest(SalePrice~LotArea+YearBuilt, data=splitdata$train)
mae(model=fitRandomForest, data=splitdata$train)
# Competition between models adding more variables
fit2<- rpart(SalePrice~LotArea+YearBuilt+HouseStyle+BedroomAbvGr+GarageCars, data=splitdata$train)
mae(model=fit2, data=splitdata$train)
fitRandomForest <- randomForest(SalePrice~LotArea+YearBuilt+HouseStyle+BedroomAbvGr+GarageCars, data=splitdata$train)
mae(model=fitRandomForest, data=splitdata$train)
# Predict values in test set
pred <- predict(fitRandomForest, newdata=splitdata$test)
head(pred)