<jupyter_start><jupyter_text>Boston Housing   load data<jupyter_code># load data
dataset = read.csv('Boston_Housing.csv')
dataset <- dataset[ -c(1) ] # drop the first column (index)

# import library 
#install.packages('MLmetrics')
library(caTools)
library(MLmetrics) # to calculate cost function for models<jupyter_output><empty_output><jupyter_text>Split dataset<jupyter_code>set.seed(2)

# split data with ratio 75/25
split = sample.split(dataset$target, SplitRatio = 0.75)
training_set = subset(dataset, split == TRUE) # Set True for train set
test_set = subset(dataset, split == FALSE) # Set false for Test set

head(training_set)<jupyter_output><empty_output><jupyter_text>Create linear model using all features<jupyter_code># create regressor using all features 
regressor = lm(formula = target ~ .,
               data = training_set)

# show the summary and coefficients for regressor
cat("--------------------------------------------------\n")
summary(regressor)
cat("--------------------------------------------------\n")
regressor$coefficients

# use model to predict the y_test
y_pred <- predict(regressor, newdata = test_set)
y_actual <- test_set$target 
cat("--------------------------------------------------\n")
cat("Linear Regression model using all features \nThe results for MAE: ",MAE(y_actual, y_pred),
    "\nThe results for MSE: ",MSE(y_actual, y_pred),"\n")<jupyter_output>--------------------------------------------------<jupyter_text>Create linear model using the most affected features<jupyter_code>regressor2 = lm(formula = target ~ ZN + RM +DIS ,data = training_set)

# show the summary and coefficients for regressor
cat("--------------------------------------------------\n")
summary(regressor2)
cat("--------------------------------------------------\n")
regressor2$coefficients

# use model to predict the y_test
y_pred_2 <- predict(regressor2, newdata = test_set)
cat("--------------------------------------------------\n")
cat("Linear Regression model using the most affected features\nThe results for MAE: "
    ,MAE(y_actual, y_pred_2),
    "\nThe results for MSE: ",MSE(y_actual, y_pred_2),"\n")<jupyter_output>--------------------------------------------------
{'fraction_non_alphanumeric': 0.27623807360290775, 'fraction_numerical': 0.007269422989550204, 'mean_word_length': 2.3013493253373314, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '158565', 'license': 'MIT', 'n_tokens_mistral': 638, 'n_tokens_neox': 605, 'n_words': 168, 'path': 'Machine Learning/Week 2/Day8_Boston-Housing-Price-Predict/code/Boston Housing - R.ipynb', 'repo_name': 'ZarahShibli/Data-Science-Bootcamp', 'size': '2201'}