DA All
DA All
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:
print(R.version.string)
Output:
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:
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)))
Output:
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:
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:
R Solution Code:
print(sum(51:91))
Output:
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.
You can replace the sample data with your own dataset, and this code will perform linear regression
on your data.
Experiment No. 6
1. To Install and load the e1071 package, run below command on R-Console:
install.packages("e1071")
library(e1071)
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:
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").
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, ])
Output:
Accuracy: 0.05
Precision: 0.0625
Recall: 1
Experiment No. 7
A matrix (data) is created with two columns (X1 and X2) and 100 rows, filled with
random numbers.
The kmeans function is used to apply the k-means clustering algorithm to the data. The
centers parameter specifies the number of clusters.
The results are visualized using a scatter plot. Each point is colored according to its
assigned cluster.
R-Code:
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)