Question
I am trying to program in R and I keep getting the following error after the randomForest part runs: Error in randomForest.default(m, y, ...) :
I am trying to program in R and I keep getting the following error after the randomForest part runs:
Error in randomForest.default(m, y, ...) : Need at least two classes to do classification.
library('ggplot2') # visualization library('ggthemes') # visualization library('scales') # visualization library('dplyr') # data manipulation library('mice') # imputation library('randomForest') # classification algorithm library('caret') library('caTools')
#load weather_dummyvariables weather_data <- read.csv(file.choose(), sep = ',', header = TRUE)
#see summary of data summary(weather_data)
set.seed(123) #to get repeatable data
train <- weather_data[1:20,] test <- weather_data[21:66,]
#random forest model rf_model <- randomForest(factor(rain) ~ dew_pt + pressure + scattered_clouds + overcast, data = train)
# Show model error plot(rf_model, ylim=c(0,0.36)) legend('topright', colnames(rf_model$err.rate), col=1:3, fill=1:3)
# Get importance importance <- importance(rf_model) varImportance <- data.frame(Variables = row.names(importance), Importance = round(importance[ ,'MeanDecreaseGini'],2))
# Create a rank variable based on importance rankImportance <- varImportance %>% mutate(Rank = paste0('#',dense_rank(desc(Importance))))
# Use ggplot2 to visualize the relative importance of variables ggplot(rankImportance, aes(x = reorder(Variables, Importance), y = Importance, fill = Importance)) + geom_bar(stat='identity') + geom_text(aes(x = Variables, y = 0.5, label = Rank), hjust=0, vjust=0.55, size = 4, colour = 'red') + labs(x = 'Variables') + coord_flip() + theme_few()
# Predict using the test set prediction <- predict(rf_model, test)
# Save the solution to a dataframe with two columns: date and rain (prediction) solution <- data.frame(WeatherID = test$date, rain = prediction)
# Write the solution to file write.csv(solution, file = 'rf_mod_Solution.csv', row.names = F)
------------this is my dataset
date | temp | dew_pt | hum | wind_spd | wind_gust | dir | vis | pressure | wind_chill | heat_index | precip | cond | scattered_clouds | clear | mostly_cloudy | overcast | fog | rain | snow | hail | thunder | tor0do |
4/28/18 0:15 | 51.6 | 48.2 | 88 | 0 | 0 | North | 7 | 29.78 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 0:35 | 51.8 | 48.7 | 89 | 0 | 0 | North | 7 | 29.79 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 0:55 | 51.8 | 48.6 | 89 | 0 | 0 | North | 7 | 29.79 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 1:15 | 51.4 | 48.2 | 89 | 0 | 0 | North | 10 | 29.8 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 1:35 | 51.1 | 48.2 | 90 | 0 | 0 | North | 10 | 29.8 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 1:55 | 50.9 | 48.2 | 90 | 0 | 0 | North | 7 | 29.79 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 2:15 | 50.5 | 47.7 | 90 | 0 | 0 | North | 7 | 29.79 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 2:35 | 48.7 | 46.2 | 91 | 0 | 0 | North | 3 | 29.79 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 2:55 | 49.8 | 47.5 | 92 | 0 | 0 | North | 5 | 29.8 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 3:15 | 50.9 | 48.6 | 92 | 0 | 0 | North | 5 | 29.81 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 3:35 | 50.7 | 48.2 | 91 | 0 | 0 | North | 5 | 29.81 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 3:55 | 50 | 47.5 | 91 | 0 | 0 | North | 5 | 29.8 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 4:15 | 50.5 | 48.2 | 92 | 0 | 0 | North | 7 | 29.78 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 4:35 | 50 | 47.7 | 92 | 0 | 0 | North | 5 | 29.78 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 4:55 | 50.2 | 48.2 | 93 | 0 | 0 | North | 4 | 29.79 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 5:15 | 50.2 | 47.8 | 92 | 0 | 0 | North | 5 | 29.79 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 5:35 | 49.8 | 47.7 | 92 | 0 | 0 | North | 4 | 29.8 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 5:55 | 50 | 47.8 | 92 | 0 | 0 | North | 4 | 29.8 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 6:15 | 50 | 47.8 | 92 | 0 | 0 | North | 3 | 29.81 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 6:35 | 50 | 48.2 | 94 | 0 | 0 | North | 2.5 | 29.82 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 6:55 | 50.4 | 48.4 | 93 | 0 | 0 | North | 3 | 29.83 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 7:15 | 50.4 | 48.4 | 93 | 0 | 0 | North | 3 | 29.83 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 7:35 | 50.9 | 48.7 | 92 | 0 | 0 | North | 3 | 29.84 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 7:55 | 50.9 | 48.6 | 92 | 0 | 0 | North | 3 | 29.84 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 8:15 | 51.4 | 49.1 | 92 | 0 | 0 | North | 3 | 29.85 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 9:15 | 52.3 | 49.8 | 91 | 0 | 0 | North | 4 | 29.85 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 9:35 | 53.6 | 50.5 | 89 | 0 | 0 | North | 5 | 29.85 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 9:55 | 53.6 | 50.2 | 88 | 0 | 0 | North | 5 | 29.85 | 0 | 0 | 0 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 10:15 | 55.4 | 51.1 | 85 | 0 | 0 | North | 7 | 29.85 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 10:35 | 57.4 | 52 | 82 | 0 | 0 | North | 7 | 29.85 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 10:55 | 58.8 | 51.4 | 76 | 4.6 | 0 | South | 10 | 29.85 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 11:15 | 60.1 | 51.6 | 74 | 0 | 0 | North | 10 | 29.85 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 11:35 | 61 | 51.1 | 70 | 0 | 0 | North | 7 | 29.84 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 11:55 | 61.5 | 51.6 | 70 | 0 | 0 | North | 10 | 29.83 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 12:15 | 62.6 | 52 | 68 | 0 | 0 | North | 10 | 29.83 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 12:35 | 64.8 | 52 | 63 | 10.4 | 0 | SSE | 10 | 29.83 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 12:55 | 63.7 | 51.4 | 64 | 10.4 | 0 | SE | 10 | 29.82 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 13:15 | 64.4 | 51.6 | 63 | 8.1 | 0 | SSE | 10 | 29.82 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 13:35 | 64.9 | 51.4 | 62 | 9.2 | 0 | SSE | 10 | 29.82 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 13:55 | 64.8 | 51.3 | 62 | 8.1 | 0 | SSE | 10 | 29.82 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 14:15 | 65.1 | 50.9 | 60 | 9.2 | 0 | SE | 10 | 29.81 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 14:35 | 63 | 50.2 | 63 | 11.5 | 0 | SE | 10 | 29.81 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 14:55 | 63 | 49.8 | 62 | 10.4 | 0 | SE | 10 | 29.8 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 15:15 | 65.1 | 49.6 | 57 | 12.7 | 0 | ESE | 10 | 29.79 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 15:35 | 66 | 49.1 | 54 | 12.7 | 0 | SSE | 10 | 29.78 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 16:15 | 66.2 | 48.9 | 54 | 10.4 | 0 | SSE | 10 | 29.76 | 0 | 0 | 0 | Clear | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 16:35 | 64.4 | 48.6 | 56 | 10.4 | 16.1 | SSE | 10 | 29.75 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 16:55 | 60.3 | 50.5 | 70 | 12.7 | 0 | SE | 5 | 29.76 | 0 | 0 | 0.03 | Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 17:15 | 59 | 50.7 | 74 | 9.2 | 0 | SE | 10 | 29.75 | 0 | 0 | 0 | Light Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 17:35 | 59.9 | 50.2 | 70 | 12.7 | 17.3 | SE | 10 | 29.74 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 17:55 | 59.4 | 50.2 | 72 | 6.9 | 0 | SE | 10 | 29.74 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 18:15 | 60.6 | 50.4 | 69 | 6.9 | 0 | SSE | 10 | 29.73 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 18:35 | 61.3 | 50.2 | 67 | 10.4 | 0 | SSE | 10 | 29.73 | 0 | 0 | 0 | Mostly Cloudy | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 19:15 | 57.2 | 50 | 77 | 8.1 | 0 | SSE | 10 | 29.73 | 0 | 0 | 0 | Scattered Clouds | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 19:35 | 58.1 | 50.5 | 76 | 15 | 31.1 | NNW | 10 | 29.81 | 0 | 0 | 0 | Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 19:55 | 55.2 | 50.2 | 83 | 10.4 | 0 | North | 5 | 29.83 | 0 | 0 | 0.04 | Light Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 20:55 | 54.5 | 50.7 | 87 | 5.8 | 0 | North | 4 | 29.86 | 0 | 0 | 0.08 | Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 21:15 | 54.5 | 50.9 | 88 | 4.6 | 0 | NE | 7 | 29.85 | 0 | 0 | 0.01 | Light Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 21:35 | 54.3 | 51.1 | 89 | 0 | 0 | North | 7 | 29.86 | 0 | 0 | 0.02 | Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 21:55 | 54.5 | 51.1 | 88 | 0 | 0 | North | 7 | 29.86 | 0 | 0 | 0.03 | Light Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 22:15 | 54.5 | 50.9 | 88 | 8.1 | 0 | East | 10 | 29.85 | 0 | 0 | 0.02 | Light Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 22:35 | 54.5 | 50.9 | 88 | 0 | 0 | North | 10 | 29.86 | 0 | 0 | 0.02 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 22:55 | 54.5 | 50.4 | 86 | 0 | 0 | North | 10 | 29.88 | 0 | 0 | 0.02 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 23:15 | 54.5 | 50.9 | 88 | 0 | 0 | North | 7 | 29.89 | 0 | 0 | 0.01 | Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
4/28/18 23:35 | 54.1 | 51.1 | 89 | 0 | 0 | North | 10 | 29.9 | 0 | 0 | 0.01 | Overcast | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
4/28/18 23:55 | 54 | 51.1 | 90 | 0 | 0 | North | 7 | 29.88 | 0 | 0 | 0.03 | Light Rain | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started