0% found this document useful (0 votes)
6 views15 pages

DA All

The document contains a series of R programming experiments covering various topics such as user input, object details, vector operations, linear regression, SVM classification, clustering, and ARIMA modeling. Each experiment includes R solution code, expected outputs, and explanations of the procedures used. The document serves as a practical guide for implementing these statistical and machine learning techniques in R.

Uploaded by

VMV CREATION
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)
6 views15 pages

DA All

The document contains a series of R programming experiments covering various topics such as user input, object details, vector operations, linear regression, SVM classification, clustering, and ARIMA modeling. Each experiment includes R solution code, expected outputs, and explanations of the procedures used. The document serves as a practical guide for implementing these statistical and machine learning techniques in R.

Uploaded by

VMV CREATION
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/ 15

Experiment No.

1
a. Write an R program to take input from the user (name and age) and display
the values. Also print the version of R.

R Solution Code:

name=readline(prompt="Input your name: ")


age=readline(prompt="Input your age: ")
print(paste("My name is",name,"and I am",age,"years
old."))

print(R.version.string)

Output:

Input your name: someone


Input your age: 19
[1] "My name is someone and I am 19 years old."
[1] "R version 3.4.4 (2018-03-15)"
b. Write an R program to get the details of the objects in memory.

R Solution Code:

name="R Programming";
n1 =10;
n2 =0.5
nums= c(10,20,30,40,50,60)
print(ls())
print("Details of the objects in memory:")
print(ls.str())

Output:

[1] "n1" "n2" "name" "nums"


[1] "Details of the objects in memory:"
n1: num 10
n2: num 0.5
name: chr "R Programming"
nums: num [1:6] 10 20 30 40 50 60
Experiment No. 2
a. Write an R program to find the maximum and the minimum value of a given
vector.

R Solution Code:

nums= c(10,20,30,40,50,60)
print('Original vector:')
print(nums)
print(paste("Maximum value of the said vector:",max(nums)))

print(paste("Minimum value of the said vector:",min(nums)))

Output:

[1] "Original vector:"


[1] 10 20 30 40 50 60
[1] "Maximum value of the said vector: 60"
[1] "Minimum value of the said vector: 10"
b. Write an R program to create three vectors a, b, c with 3 integers. Combine
the three vectors to become a 3×3 matrix where each column represents a
vector. Print the content of the matrix.

R Solution Code:

a<-c(1,2,3)
b<-c(4,5,6)
c<-c(7,8,9)
m<-cbind(a,b,c)
print("Content of the said matrix:")

print(m)

Output:

[1] "Content of the said matrix:"


a b c
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
Experiment No. 3
a. Write an R program to create a vector which contains 10 random integer
values between -50 and +50.

R Solution Code:

v =sample(-50:50,10, replace=TRUE)
print("Content of the vector:")
print("10 random integer values between -50 and +50:")
print(v)

Output:

[1] "Content of the vector:"


[1] "10 random integer values between -50 and +50:"
[1] 31 -13 -21 42 49 -39 20 12 39 -2
b. Write an R program to create a sequence of numbers from 20 to 50 and
find the mean of numbers from 20 to 60 and sum of numbers from 51 to
91.

R Solution Code:

print("Sequence of numbers from 20 to 50:")


print(seq(20,50))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")

print(sum(51:91))

Output:

[1] "Sequence of numbers from 20 to 50:"


[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 4445 46 47 48 49 50
[1] "Mean of numbers from 20 to 60:"
[1] 40
[1] "Sum of numbers from 51 to 91:"
[1] 2911
Experiment No. 4
 Write an R program to create a simple bar plot of five subjects’ marks.

R Solution Code:

marks= c(70,95,80,74,70)
barplot(marks,
main="Comparing marks of 5 subjects",
xlab="Marks",
ylab="Subject",
names.arg=c("DA","ANN&FL","DBMS_DS", "DWH","RET"),
col="darkred",

horiz= FALSE)

Output:
Experiment No. 5
 Write an R Code to find Linear Regression.

Procedure & R-Code:


In R, you can find linear regression using the lm function, which stands for "linear model."

 Here's a simple algorithm using lm:


# Generate some sample data
set.seed(123)
x <- 1:10
y <- 2 * x + rnorm(10)

# Fit a linear regression model


linear_model <- lm(y ~ x)

# Print the summary of the linear regression model


summary(linear_model)

# Make predictions with the model


new_data <- data.frame(x = 11:15)
predictions <- predict(linear_model, newdata = new_data)

# Print the predictions


cat("Predictions for new data:\n")
print(predictions)

# Plot the original data and the regression line


plot(x, y, main = "Linear Regression Example", xlab = "x", ylab = "y")
abline(linear_model, col = "red")
Output:
1 2 3 4 5
21.62378 23.54181 25.45984 27.37787 29.29590
Here,
1. We generate some sample data where y is a linear function of x with some random noise.
2. We use the lm function to fit a linear regression model (linear_model) to the data.
3. We print the summary of the linear regression model using summary(linear_model).
4. We make predictions for new data points (new_data) using the predict function.
5. We plot the original data points and the regression line using plot and abline function.

You can replace the sample data with your own dataset, and this code will perform linear regression
on your data.
Experiment No. 6

 Write an R Code to perform Support Vector Machine (SVM) Classification.


Procedure & R-Code:
To perform Support Vector Machine (SVM) classification in R, you can use the e1071
package, which provides the svm function.

1. To Install and load the e1071 package, run below command on R-Console:
install.packages("e1071")
library(e1071)

2. Generate or load your data:


Suppose you have a dataset with features X1 and X2 and corresponding labels
y (0 or 1 for binary classification).

 Here's an example algorithm using SVM for classification:

3. Split the data into training and testing sets (optional):


set.seed(123)
data <- data.frame(X1 = rnorm(100), X2 = rnorm(100), y = sample(0:1, 100, replace =
TRUE))

# Split into training (80%) and testing (20%) sets


train_indices <- sample(1:nrow(data), 0.8 * nrow(data))
train_data <- data[train_indices, ]
test_data <- data[-train_indices, ]

Here, the set.seed() function is used to set the seed for the random number generator. This
ensures that when you generate random numbers or perform any operation that involves
randomness, the results are reproducible.

Setting the seed with set.seed() ensures that the sequence of random numbers generated is the
same every time you run the code. This is crucial for reproducibility, as it allows you or
others to obtain the same results when running the code, making your analyses or
experiments more transparent and reliable.
4. Train the SVM model:

# Assuming binary classification


svm_model <- svm(y ~ X1 + X2, data = train_data, kernel = "linear", cost = 1)

Here, y ~ X1 + X2 specifies the formula for the SVM, and kernel = "linear" indicates a linear
kernel. You can explore other kernels such as radial basis function ("radial") or polynomial
("polynomial").

5. Make predictions on the test set:


predictions <- predict(svm_model, newdata = test_data)

6. Evaluate the model performance:

Depending on the nature of your problem (classification or regression), you can use
appropriate evaluation metrics.

For classification, you might use metrics like accuracy, precision, recall, or the confusion
matrix:
confusion_matrix <- table(predictions, test_data$y)
accuracy <- sum(diag(confusion_matrix)) / sum(confusion_matrix)
precision <- confusion_matrix[2, 2] / sum(confusion_matrix[, 2])
recall <- confusion_matrix[2, 2] / sum(confusion_matrix[2, ])

cat("Accuracy:", accuracy, "\n")


cat("Precision:", precision, "\n")
cat("Recall:", recall, "\n")

Output:
Accuracy: 0.05
Precision: 0.0625
Recall: 1
Experiment No. 7

 Write an R Code to demonstrate clustering method in R programming


Procedure:
1. Generate Sample Data:

A matrix (data) is created with two columns (X1 and X2) and 100 rows, filled with
random numbers.

2. Apply k-Means Clustering:

The kmeans function is used to apply the k-means clustering algorithm to the data. The
centers parameter specifies the number of clusters.

3. Visualize the Results:

The results are visualized using a scatter plot. Each point is colored according to its
assigned cluster.

4. Print Cluster Centers:

The cluster centers are printed to the console.

R-Code:

# Install and load necessary packages (if not already installed)


# install.packages("ggplot2")
# install.packages("cluster")
library(ggplot2)
library(cluster)

# Generate sample data


set.seed(123)
data <- matrix(rnorm(200), ncol = 2)

# Apply k-means clustering


k <- 3 # Number of clusters
kmeans_result <- kmeans(data, centers = k)

# Visualize the clustering results


data_df <- data.frame(data, Cluster = as.factor(kmeans_result$cluster))
# Scatter plot
ggplot(data_df, aes(x = X1, y = X2, color = Cluster)) +
geom_point() +
ggtitle("K-Means Clustering Results") +
theme_minimal()

# Print the cluster centers


cat("Cluster Centers:\n")
print(kmeans_result$centers)

Output:
Experiment No. 8
 Write an R code to fit an ARIMA model to a time series data.
Time series forecasting is a process of predicting future values with the help of
some statistical tools and methods used on a data set with historical data.

ARIMA stands for Autoregressive Integrated Moving Average. It's a popular time series
forecasting model that captures the temporal structure and patterns in the data. In R,
the ARIMA model is implemented through the arima function in the stats package and
the auto.arima function in the forecast package.

Procedure:
1. Load the forecast library, which provides functions for time series analysis
including ARIMA modeling.
2. Generate or load time series data. Simulate a time series using the arima.sim
function with a specific ARIMA(1,1,1) structure.
3. Fit an ARIMA model to the time series data using the auto.arima function.
This function automatically selects the best ARIMA model based on Akaike
Information Criterion (AIC) criterion.
4. Print the summary of the ARIMA model using the summary function.
5. Plot the time series data along with the forecasts for the next 10 periods using
the plot function.

R-Code:
# Load required libraries
library(forecast)

# Generate or load time series data (example using simulated data)


set.seed(123)
ts_data <- arima.sim(list(order = c(1, 1, 1), ar = 0.7, ma = -0.4), n = 100)

# Fit ARIMA model


arima_model <- auto.arima(ts_data)

# Summary of the ARIMA model


summary(arima_model)

# Plot time series data and forecasts


plot(forecast(arima_model, h = 10)) # Forecasting 10 periods ahead
Output:

You might also like