The table in the data has four columns, id, carat, cut and probability. the probability is created at random and every time there is an association with that value the probability will go up by 0.01. There are ten diamonds inserted into the table using the code below. Keep in mind that this will not work if you are not connected to the database. Also if you are using MAMP, make sure your servers are up and running. To update the probability in the table use, update “table name” set “column name”, “column name” where probability = value.
Once the table has all the values and the probabilities are updated the next step is to make inferences. and insert those inferences into another table called “DiamondsToTrack”. The diamonds that are being inserted into this table are diamonds that have a probability that is greater than 50% after the calculations.
library(RMySQL)
## Warning: package 'RMySQL' was built under R version 4.0.3
## Loading required package: DBI
data <- read.csv("~/CST-425/diamonds.csv")
mysqlconnection = dbConnect(MySQL(), user = 'root', password = 'root', dbname = 'diamonds', host = 'localhost')
#Inserting values into table
#dbSendQuery(mysqlconnection,"insert into diamonds_table(id, carat, cut, probability) values(1,0.23,'very good', 0.2)")
#dbSendQuery(mysqlconnection,"insert into diamonds_table(id, carat, cut, probability) values(2,0.31,'ideal', 0.4)")
#dbSendQuery(mysqlconnection, "insert into diamonds_table(id, carat, cut, probability) values(3,0.42,'premium', 0.5)")
#dbSendQuery(mysqlconnection, "insert into diamonds_table(id, carat, cut, probability) values(4,0.26,'very good', 0.3)")
#dbSendQuery(mysqlconnection,"insert into diamonds_table(id, carat, cut, probability) values(5,0.91,'fair', 0.4)")
#dbSendQuery(mysqlconnection, "insert into diamonds_table(id, carat, cut, probability) values(6,0.80,'ideal', 0.5)")
#dbSendQuery(mysqlconnection, "insert into diamonds_table(id, carat, cut, probability) values(7,0.7,'very good', 0.3)")
#dbSendQuery(mysqlconnection, "insert into diamonds_table(id, carat, cut, probability) values(8,0.71,'premium', 0.6)")
#dbSendQuery(mysqlconnection, "insert into diamonds_table(id, carat, cut, probability) values(9,0.3,'premium', 0.4)")
#dbSendQuery(mysqlconnection, "insert into diamonds_table(id, carat, cut, probability) values(10,0.34,'ideal', 0.7)")
#-------------------------------------------------------#
#updating the probabilistic values in the table
#dbSendQuery(mysqlconnection, "UPDATE diamonds_table SET carat = 0.23 and cut = 'very good' WHERE probability = 0.23")
#dbSendQuery(mysqlconnection,"UPDATE diamonds_table SET carat = 0.26 and cut = 'very good' WHERE probability = 0.36")
#dbSendQuery(mysqlconnection,"update diamonds_table set carat = 0.91, cut = 'fair' where probability = 0.42")
#dbSendQuery(mysqlconnection,"update diamonds_table set carat = 0.8, cut = 'ideal' where probability = 0.51")
#dbSendQuery(mysqlconnection,"update diamonds_table set carat = 0.7, cut = 'very good' where probability = 0.39")
#dbSendQuery(mysqlconnection, "UPDATE diamonds_table SET carat = 0.71 and cut = 'premium' WHERE probability = 0.85")
#dbSendQuery(mysqlconnection, "UPDATE diamonds_table SET carat = 0.3 and cut = 'premium' WHERE probability = 0.42")
#dbSendQuery(mysqlconnection, "UPDATE diamonds_table SET carat = 0.34 and cut = 'ideal' WHERE probability = 0.73")
#--------------------------------------------------------#
#Showing the diamond_table
result1 = dbSendQuery(mysqlconnection, "select * from diamonds_table")
data.frame1 = fetch(result1)
print(data.frame1)
## id carat cut probability
## 1 1 0.23 very good 0.23
## 2 2 0.31 ideal 0.40
## 3 3 0.42 premium 0.50
## 4 4 0.26 very good 0.36
## 5 5 0.91 fair 0.41
## 6 6 0.80 ideal 0.50
## 7 7 0.70 very good 0.32
## 8 8 0.71 premium 0.85
## 9 9 0.30 premium 0.42
## 10 10 0.34 ideal 0.73
#calculating the average
#mean(x, trim = 0, na.rm = FALSE)
#p1 = 0.85
#p2 = 0.5
#x <-c(p1, p2)
#result.mean <- mean(x)
#print(result.mean)
#Inserting values into the second table
#dbSendQuery(mysqlconnection, "insert into diamondstotrack(id, carat, cut, probability) values(1, 0.71, 'premium', 0.90)")
#dbSendQuery(mysqlconnection, "insert into diamondstotrack(id, carat, cut, probability) values(2, 0.34, 'premium', 0.85)")
#dbSendQuery(mysqlconnection, "insert into diamondstotrack(id, carat, cut, probability) values(3, 0.8, 'ideal', 0.6)")
#dbSendQuery(mysqlconnection, "insert into diamondstotrack(id, carat, cut, probability) values(4, 0.42, 'premium', 0.6)")
result2 = dbSendQuery(mysqlconnection, "select * from diamondstotrack")
data.frame2 = fetch(result2)
print(data.frame2)
## id carat cut probability
## 1 1 0.71 premium 0.90
## 2 2 0.34 premium 0.85
## 3 3 0.80 ideal 0.60
## 4 4 0.42 premium 0.60
#Conclusion
There four diamonds that are added to the table “DiamondsToTrack”. Two of those diamonds have a probability that is greater than 80%. The carat of the first diamonds is 0.34 the cut is premium. The carat for the other diamond is 0.71 its cut is also premium. These two diamonds are ones. These two diamonds are added to this table because of they have a probability that is greater than or equal to 85%. the other two diamonds have probability that is greater than 50%.