0% found this document useful (0 votes)
1 views7 pages

Rlab

The document outlines various statistical and machine learning techniques using R, including generating random data, performing t-tests, fitting linear regression models, logistic regression, k-means clustering, collaborative filtering for recommendations, sentiment analysis, random forests, and time series forecasting. Each section includes code snippets for data generation, model fitting, predictions, and visualizations. The document serves as a comprehensive guide for implementing these methods in R.

Uploaded by

PRIYADHARSHINI E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Rlab

The document outlines various statistical and machine learning techniques using R, including generating random data, performing t-tests, fitting linear regression models, logistic regression, k-means clustering, collaborative filtering for recommendations, sentiment analysis, random forests, and time series forecasting. Each section includes code snippets for data generation, model fitting, predictions, and visualizations. The document serves as a comprehensive guide for implementing these methods in R.

Uploaded by

PRIYADHARSHINI E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

7.

A:
# Generate random data
data_group1 <- rnorm(50, mean = 10, sd = 2)
data_group2 <- rnorm(50, mean = 12, sd = 2)

# Descriptive statistics
mean_group1 <- mean(data_group1)
mean_group2 <- mean(data_group2)
sd_group1 <- sd(data_group1)
sd_group2 <- sd(data_group2)

# T-test
t_test_result <- t.test(data_group1, data_group2)

# Display results
cat("Descriptive Statistics:\n")
cat("Group 1 - Mean:", mean_group1, "SD:", sd_group1, "\n")
cat("Group 2 - Mean:", mean_group2, "SD:", sd_group2, "\n\n")

cat("T-Test Results:\n")
cat("t-value:", t_test_result$statistic, "\n")
cat("p-value:", t_test_result$p.value, "\n")

# Plot histogram
hist(data_group1, main = "Group 1 Histogram", xlab = "Values", col = "lightblue")
hist(data_group2, main = "Group 2 Histogram", xlab = "Values", col = "lightgreen")

7.B:
# Generate some example data
set.seed(123)
x <- rnorm(100, mean = 10, sd = 2)
y <- 3 * x + rnorm(100, mean = 0, sd = 5)

# Create a data frame


data <- data.frame(x, y)

# Fit a linear regression model


model <- lm(y ~ x, data = data)

# Print model summary


summary(model)

# Make predictions using the model


new_data <- data.frame(x = c(12, 15, 18))
predictions <- predict(model, newdata = new_data)

# Display predictions
cat("Predictions:\n", predictions)

# Plot the data and regression line


plot(x, y, main = "Linear Regression Example", xlab = "X", ylab = "Y")
abline(model, col = "red")

8.A
# Load the Iris dataset
data(iris)

# Split the dataset into training and testing sets


set.seed(123)
indices <- sample(1:nrow(iris), 0.7 * nrow(iris)) # 70% for training, 30% for testing
train_data <- iris[indices, ]
test_data <- iris[-indices, ]
# Create a logistic regression model
model <- glm(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data =
train_data, family = "binomial")

# Print model summary


summary(model)

# Make predictions on the test set


predictions <- predict(model, newdata = test_data, type = "response")

# Convert probabilities to predicted classes


predicted_classes <- ifelse(predictions > 0.5, "versicolor", "setosa")

# Evaluate model performance


confusion_matrix <- table(predicted_classes, test_data$Species)
cat("Confusion Matrix:\n", confusion_matrix, "\n")

# Calculate accuracy
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
cat("Accuracy:", accuracy, "\n")

8.B:
# Load the Iris dataset
data(iris)

# Selecting relevant features


features <- iris[, c("Sepal.Length", "Sepal.Width")]

# Set seed for reproducibility


set.seed(123)

# Perform k-means clustering with k=3 (assuming three clusters)


k <- 3
kmeans_result <- kmeans(features, centers = k)

# Display cluster assignments


cat("Cluster Assignments:\n", kmeans_result$cluster, "\n")

# Plot the clusters


plot(features, col = kmeans_result$cluster, main = "K-Means Clustering", xlab = "Sepal
Length", ylab = "Sepal Width")
points(kmeans_result$centers, col = 1:k, pch = 8, cex = 2, lwd = 2)

9.
# Install and load necessary packages
if (!requireNamespace("recommenderlab", quietly = TRUE))
install.packages("recommenderlab")
library(recommenderlab)

# Load a different dataset (using built-in 'MovieLense' dataset as an example)


data("MovieLense")
movie_ratings <- as(MovieLense, "realRatingMatrix")

# Split the data into training and testing sets


set.seed(123)
train_test <- evaluationScheme(movie_ratings, method = "split", train = 0.8, given = 5)

# Build a recommendation model using collaborative filtering (user-based)


recommender_model <- Recommender(data = getData(train_test, "train"), method =
"UBCF")

# Generate recommendations for users in the test set


recommendations <- predict(recommender_model, newdata = getData(train_test, "known"), n
= 5)
# Print the recommendations for the first user
print(recommendations@items[[1]])

10.
# Install necessary packages
if (!requireNamespace("SentimentAnalysis", quietly = TRUE))
install.packages("SentimentAnalysis")
if (!requireNamespace("SnowballC", quietly = TRUE)) install.packages("SnowballC")

# Load packages
library(SentimentAnalysis)

# Sample text data


text_data <- c(
"I love this product! It's amazing.",
"The customer service was terrible.",
"Neutral opinion on this topic.",
"This is the best thing ever!",
"I don't like the quality of the product."
)

# Perform sentiment analysis


sentiments <- analyzeSentiment(text_data)

# Display sentiments
print(sentiments)

11.
# Install and load necessary packages
if (!requireNamespace("randomForest", quietly = TRUE)) install.packages("randomForest")
if (!requireNamespace("mlbench", quietly = TRUE)) install.packages("mlbench")
library(randomForest)
library(mlbench)

# Load the German Credit Dataset


data("GermanCredit", package = "mlbench")

# Explore the structure of the dataset


str(GermanCredit)

# Split the data into training and testing sets


set.seed(123)
sample_indices <- sample(1:nrow(GermanCredit), 0.8 * nrow(GermanCredit))
train_data <- GermanCredit[sample_indices, ]
test_data <- GermanCredit[-sample_indices, ]

# Define the target variable (response)


response_variable <- "Class"

# Build a random forest model


model <- randomForest(as.factor(Class) ~ ., data = train_data, ntree = 100)

# Make predictions on the test set


predictions <- predict(model, newdata = test_data)

# Evaluate the model


confusion_matrix <- table(predictions, test_data$Class)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)

# Print the confusion matrix and accuracy


print(confusion_matrix)
print(paste("Accuracy: ", accuracy))
12.
# Install and load necessary packages
if (!requireNamespace("forecast", quietly = TRUE)) install.packages("forecast")
library(forecast)

# Generate example time series data (replace this with your own sales data)
set.seed(123)
sales_data <- ts(rnorm(100, mean = 100, sd = 10), start = 1)

# Identify the best ARIMA model using auto.arima


best_model <- auto.arima(sales_data)

# Forecast sales for the nearby future (adjust 'h' based on your prediction horizon)
forecast_values <- forecast(best_model, h = 10)

# Plot the forecast with confidence intervals


plot(forecast_values, main = "Store Sales Forecast", xlab = "Time", ylab = "Sales",
xlim = c(min(time(sales_data)), max(time(sales_data)) + 10))
lines(sales_data, col = "blue", lty = 1) # Add original time series data for comparison
lines(forecast_values$mean, col = "red", lty = 1) # Plot forecast values in red
legend("topleft", legend = c("Original Sales", "Forecast"),
col = c("blue", "red"), lty = c(1, 1), cex = 0.8)

# Print the forecast values


print(forecast_values)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy