Regressão Linear

# Carregar pacotes
library(caret)
## Loading required package: ggplot2
## Loading required package: lattice
# Carregar dados de exemplo
data(mtcars)

# Dividir os dados em treino e teste
set.seed(123)
trainIndex <- createDataPartition(mtcars$mpg, p = .8, 
                                  list = FALSE, 
                                  times = 1)
mtcarsTrain <- mtcars[trainIndex,]
mtcarsTest  <- mtcars[-trainIndex,]

# Treinar o modelo
model <- train(mpg ~ ., data = mtcarsTrain, method = 'lm')
## Warning in predict.lm(modelFit, newdata): prediction from rank-deficient fit;
## attr(*, "non-estim") has doubtful cases
# Fazer previsões
predictions <- predict(model, mtcarsTest)

# Avaliar o modelo
postResample(predictions, mtcarsTest$mpg)
##      RMSE  Rsquared       MAE 
## 4.8089808 0.6297763 3.3848440

Cluster

# Carregar pacotes
library(cluster)

# Carregar dados de exemplo
data(iris)
irisData <- iris[, -5]

# Realizar clustering (K-means)
set.seed(123)
kmeansResult <- kmeans(irisData, centers = 3)

# Adicionar os clusters aos dados originais
iris$Cluster <- as.factor(kmeansResult$cluster)

# Visualizar os clusters
library(ggplot2)
ggplot(iris, aes(Petal.Length, Petal.Width, color = Cluster)) +
  geom_point(size = 3) +
  labs(title = "Clustering com K-means", x = "Petal Length", y = "Petal Width")