6.5.3
1a) I would not use linear because the points follow a curve 1b)I would use a parabola because it appears to curve up to a peak and the fall at a similar to rate to which it rose
2a) The equation means that 8 hours is peak sleep time 2b) Happiness starts decreasing after 8 hours
happy<-data.frame(x=c(0.00,1.00,2.00,3.00,4.00,5.00,6.00,7.00,8.00,9.00,1.01,1.11,1.21,1.31,1.41,1.51,1.61,1.71,1.81,1.91), y=c(2.89840027e0,1.808064e1,2.49052e1,3.8114241e1,4.65186e1,5.17108e1,6.316792e1,6.26684e1,6.702376e1,6.4720079e1,5.711712e1,5.324208e1,5.0189319e1,3.8284401e1,2.5770001e1,1.178748e1,2.19716003e0,-1.5252681e1,-3.3162001e1,-5.472124e1))
model <- lm(y ~ poly(x, 2), data=happy)
summary(model)
Call:
lm(formula = y ~ poly(x, 2), data = happy)
Residuals:
Min 1Q Median 3Q Max
-74.898 -9.068 4.752 13.575 41.630
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 28.763 6.767 4.250 0.00054 ***
poly(x, 2)1 82.914 30.265 2.740 0.01397 *
poly(x, 2)2 8.304 30.265 0.274 0.78711
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 30.26 on 17 degrees of freedom
Multiple R-squared: 0.3084, Adjusted R-squared: 0.227
F-statistic: 3.79 on 2 and 17 DF, p-value: 0.04353
6.6.4
install.packages("class")
trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.4/class_7.3-22.tgz'
Content type 'application/x-gzip' length 97481 bytes (95 KB)
==================================================
downloaded 95 KB
The downloaded binary packages are in
/var/folders/0c/4z4yx80965g2ykwwtvdlplth0000gn/T//RtmpdiEKlw/downloaded_packages
library(class)
classy_data<-data.frame(x=c(2,4,6,3,4,7),y=c(3,2,5,6,4,3),class=c(1,1,2,1,1,2))
train_indices <- sample(1:nrow(classy_data), size = 1*nrow(classy_data))
train_labels <- classy_data$class
train_data <- classy_data[train_indices, -3]
test_data <- classy_data[-train_indices,-3]
test_labels <- classy_data$class[-train_indices]
knn_pred <- knn(train = train_data, test = test_data, cl = train_labels , k = 3)
confusion_matrix <- table(knn_pred, test_labels)
confusion_matrix
< table of extent 2 x 0 >
I am getting an error message because train_index was not found. I would like to discuss this in office hours
With k=5 it woulld be a class 1 but I did that after plotting it visually
The confusion matrix incorrectly classified 1 virginica as a versacolor
library(class)
data("iris")
train_indices <- sample(1:nrow(iris), size = 0.7*nrow(iris))
train_data <- iris[train_indices, -5]
train_labels <- iris$Species[train_indices]
test_data <- iris[-train_index ,-5]
Error: object 'train_index' not found
I get the same error here, “Error: object ‘train_index’ not found” thus, I cannot run different k values