Experiment-1
> print("Hello R")
[1] "Hello R"
>
>
> a <- 10
> b <- 5
> sum <- a + b
> diff <- a - b
> prod <- a * b
> div <- a / b
>
> sum; diff; prod; div
[1] 15
[1] 5
[1] 50
[1] 2
>
Experiment-2
# Sample Data
> data <- [Link](Name=c("A","B","C","D"),
+ Age=c(20,25,22,30),
+ Marks=c(85,90,88,95))
>
> # View data
> print(data)
Name Age Marks
1 A 20 85
2 B 25 90
3 C 22 88
4 D 30 95
> head(data) # first rows
Name Age Marks
1 A 20 85
2 B 25 90
3 C 22 88
4 D 30 95
> str(data) # structure
'[Link]': 4 obs. of 3 variables:
$ Name : chr "A" "B" "C" "D"
$ Age : num 20 25 22 30
$ Marks: num 85 90 88 95
>
> # Manipulating
> data$Marks[2] <- 92 # Update value
> data$Grade <- c("A","A","B","A") # Add new column
> data
Name Age Marks Grade
1 A 20 85 A
2 B 25 92 A
3 C 22 88 B
4 D 30 95 A
>
Experiment-3
> # Sample Data
> data <- [Link](Name=c("A","B","C","D"),
+ Age=c(20,25,22,30),
+ Marks=c(85,90,88,95))
>
> # View data
> print(data)
Name Age Marks
1 A 20 85
2 B 25 90
3 C 22 88
4 D 30 95
> head(data) # first rows
Name Age Marks
1 A 20 85
2 B 25 90
3 C 22 88
4 D 30 95
> str(data) # structure
'[Link]': 4 obs. of 3 variables:
$ Name : chr "A" "B" "C" "D"
$ Age : num 20 25 22 30
$ Marks: num 85 90 88 95
>
> # Manipulating
> data$Marks[2] <- 92 # Update value
> data$Grade <- c("A","A","B","A") # Add new column
> data
Name Age Marks Grade
1 A 20 85 A
2 B 25 92 A
3 C 22 88 B
4 D 30 95 A
>
> # Table
> table(data$Grade)
AB
31
>
> # Bar plot
> barplot(data$Marks, [Link]=data$Name, col="skyblue")
>
> # Pie chart
> pie(data$Marks, labels=data$Name)
>
> # Scatter plot
> plot(data$Age, data$Marks, main="Age vs Marks", xlab="Age", ylab="Marks")
Experiment-4
> # Binomial distribution
> x <- 0:10
> prob <- dbinom(x, size=10, prob=0.5)
>
> # Plot PMF
> barplot(prob, [Link]=x, main="Binomial Distribution", col="orange")
>
> # Cumulative distribution
> plot(pbinom(x, size=10, prob=0.5), type="b", col="blue", main="CDF")
Experiment-5
> # Normal distribution
> x <- seq(-4,4,0.1)
> y <- dnorm(x, mean=0, sd=1)
>
> plot(x, y, type="l", col="red", main="Normal Distribution")
>
> # Poisson distribution
> x <- 0:10
> y <- dpois(x, lambda=4)
> barplot(y, [Link]=x, col="green", main="Poisson Distribution")
Experiment-6
> numbers <- c(12,15,20,22,25,30,35,40)
>
> mean(numbers)
[1] 24.875
> median(numbers)
[1] 23.5
> var(numbers)
[1] 93.26786
> sd(numbers)
[1] 9.657529
> range(numbers)
[1] 12 40
Experiment-7
# Random normal numbers
> rand_nums <- rnorm(1000, mean=50, sd=10)
>
> rand_nums;
[1] 38.59711 56.03057 57.41184 45.78230 57.87643 48.85863 56.33479 41.57319
[9] 30.75650 40.80536 58.22286 55.21781 48.17489 42.51264 56.87260 63.90394
[17] 53.24722 49.84065 46.26590 43.48728 36.30141 39.38998 48.60872 50.95458
[25] 46.03566 46.61314 51.74119 38.95834 47.80732 58.21461 45.82014 39.27179
[33] 36.09553 54.50108 55.88502 51.92239 58.62551 41.58897 68.58548 60.05453
[41] 35.81503 57.52317 39.52293 33.01108 43.55661 42.64349 39.26629 53.16199
[49] 57.15682 66.75239 59.73774 63.29174 37.31079 50.86888 44.11610 58.09880
[57] 38.43179 65.18027 51.26088 51.27480 53.26068 31.25280 48.76762 51.52395
[65] 44.10576 44.37485 46.26237 33.26881 71.18517 57.59909 14.41255 49.47458
[73] 46.63922 51.24823 44.66767 54.48741 55.31908 46.19019 66.68137 29.49272
[81] 39.92878 48.64212 57.70500 58.34083 45.04905 43.48206 41.63546 47.95936
[89] 67.90310 61.92921 59.61295 55.90178 47.27727 58.94049 46.93747 34.21972
[97] 38.08907 62.91493 54.22468 60.62359 58.35831 54.76898 76.43448 58.82341
[105] 65.40924 47.44043 52.30210 54.99639 40.76267 43.50370 44.69131 40.19097
[113] 51.36711 38.25534 62.58996 41.23682 58.61427 40.23903 43.66867 47.36309
[121] 53.57104 64.11683 58.42837 52.67382 51.43081 55.84297 33.22685 41.30579
[129] 42.30732 38.13571 45.83351 44.05357 46.00614 47.45085 48.17041 34.75275
[137] 54.97895 53.28515 45.82984 40.38629 43.68433 54.37563 56.37589 43.67930
[145] 40.00714 43.93308 50.87940 45.36193 53.24941 65.26349 47.56091 45.19792
[153] 52.59138 67.66043 45.21884 57.22561 47.70492 44.90853 28.56835 55.41096
[161] 64.76222 69.65781 54.89028 62.79330 54.63295 53.05700 65.16417 39.35856
[169] 51.25454 53.68998 32.30186 46.94858 56.10990 50.18712 47.12751 56.71820
[177] 54.60680 61.80938 46.48283 52.05037 46.75392 52.59283 44.40805 47.34117
[185] 53.62193 47.61885 50.92462 33.33874 36.56010 47.14603 53.22789 42.68393
[193] 41.97387 34.68957 55.13120 65.84175 51.20132 51.65263 61.85522 56.78921
[201] 53.53012 53.49898 57.02379 51.49186 49.92693 41.54977 44.19875 49.11051
[209] 52.43578 47.19678 54.94093 55.14533 61.04446 45.64311 56.89179 42.22171
[217] 38.74321 57.97406 23.36057 39.34143 41.79826 65.26938 48.93252 54.23720
[225] 51.78997 46.84695 44.47726 22.62225 50.66889 39.24414 42.49005 44.10702
[233] 48.82621 33.75492 52.19758 46.64948 45.85360 42.07676 59.73386 47.91164
[241] 54.19062 39.63278 49.95393 30.84685 48.67353 50.75946 49.99228 65.84163
[849] 56.97228 44.74537 34.91168 52.39629 37.56992 46.96479 48.24095 46.37252
[857] 62.22282 58.21682 30.28306 46.49649 40.25127 60.54507 35.47408 26.29564
[865] 58.60917 36.53283 24.90772 37.64288 43.38884 53.52078 39.61952 61.01944
[873] 47.34036 46.37544 39.31665 39.42202 61.58142 43.85858 47.42077 26.02593
[881] 40.03654 60.38683 51.74769 37.19864 42.33814 52.75431 51.75287 58.33647
[889] 63.09371 40.25907 33.04165 54.61292 26.31604 46.33305 34.50956 48.10476
[897] 56.32989 37.81094 55.86767 35.11537 31.17890 53.63990 40.02912 60.34102
[905] 48.37105 39.03331 45.71609 49.89465 74.05714 52.23082 50.62561 65.60013
[913] 51.58477 48.81204 48.90142 49.14920 51.35468 43.83004 74.92854 33.50138
[921] 57.97786 53.31426 50.44878 49.88660 59.37096 54.63170 60.79544 48.64849
[929] 64.59075 57.10566 44.62087 51.59346 47.92103 45.88228 68.21484 46.24188
[937] 60.59789 47.35900 50.54481 47.66913 64.81956 40.50926 49.83005 27.91189
[945] 56.54863 44.44538 49.73439 47.85849 45.20680 51.27396 56.51421 60.67062
[953] 48.71572 52.38843 38.60888 45.60436 48.17948 55.88575 43.38227 61.10806
[961] 63.00472 62.52667 44.48822 66.43695 63.22525 41.23535 51.28849 59.13916
[969] 42.54360 65.36191 57.00516 45.14366 60.04082 63.83564 29.25142 36.39597
[977] 41.11321 48.64988 54.16341 41.51300 57.09831 38.43693 56.40942 50.44369
[985] 32.97380 59.99972 60.62495 31.97340 64.18155 51.47370 63.55946 39.22332
[993] 48.53317 56.86470 51.69003 60.41895 32.06982 49.94586 47.47141 64.23404
>
> # Histogram
> hist(rand_nums, col="purple", main="Histogram of Random Numbers")
>
> # Density
> plot(density(rand_nums), col="blue", main="Density Plot")
>
Experiment-9
# Generate random numbers
> [Link](123) # for reproducibility
> x <- rnorm(50, mean=50, sd=10) # 50 random numbers from normal distribution
> y <- x + rnorm(50, mean=0, sd=5) # y depends on x with some random noise
>
> # Scatter plot
> plot(x, y,
+ main="Scatter Plot of Random Numbers",
+ xlab="X values",
+ ylab="Y values",
+ col="blue", pch=19)
>
> # Correlation
> correlation_value <- cor(x, y)
> print(paste("Correlation between X and Y:", correlation_value))
[1] "Correlation between X and Y: 0.895363537296679"
Experiment-9
### (A) Contingency Table and Chi-Square Test
>
> # Sample data: Survey of Male/Female responses (Yes/No/Maybe)
> data <- matrix(c(50, 30, 20,
+ 40, 25, 35),
+ nrow = 2, byrow = TRUE)
>
> # Assign row and column names
> rownames(data) <- c("Male", "Female")
> colnames(data) <- c("Yes", "No", "Maybe")
>
> # Print contingency table
> print("Contingency Table:")
[1] "Contingency Table:"
> print(data)
Yes No Maybe
Male 50 30 20
Female 40 25 35
>
> # Chi-square test of independence
> chi_result <- [Link](data)
> print("Chi-Square Test Result:")
[1] "Chi-Square Test Result:"
> print(chi_result)
Pearson's Chi-squared test
data: data
X-squared = 5.6566, df = 2, p-value = 0.05911
>
>
> ### (B) Chi-Square Goodness of Fit
>
> # Example: Rolling a die 60 times, observed counts
> observed <- c(8, 9, 10, 11, 12, 10)
>
> # Expected counts (fair die → each outcome equally likely)
> expected <- rep(60/6, 6)
>
> # Perform chi-square goodness of fit test
> gof_result <- [Link](observed, p = rep(1/6, 6))
>
> print("Goodness of Fit Test Result:")
[1] "Goodness of Fit Test Result:"
> print(gof_result)
Chi-squared test for given probabilities
data: observed
X-squared = 1, df = 5, p-value = 0.9626
Experiment-10
# Linear Regression
> x <- c(1,2,3,4,5,6,7,8,9,10)
> y <- c(2,4,5,7,9,10,12,14,15,18)
> model <- lm(y ~ x)
> summary(model)
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-0.58182 -0.28636 0.02727 0.22273 0.70909
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.20000 0.28762 0.695 0.507
x 1.70909 0.04635 36.870 3.21e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.421 on 8 degrees of freedom
Multiple R-squared: 0.9941, Adjusted R-squared: 0.9934
F-statistic: 1359 on 1 and 8 DF, p-value: 3.211e-10
> plot(x,y)
> abline(model, col="red")
>
> # Logistic Regression
> data <- [Link](
+ exam_score=c(45,50,55,60,65,70,75,80,85,90),
+ pass=factor(c(0,0,0,0,1,1,1,1,1,1))
+)
> log_model <- glm(pass ~ exam_score, data=data, family=binomial)
Warning messages:
1: [Link]: algorithm did not converge
2: [Link]: fitted probabilities numerically 0 or 1 occurred
> summary(log_model)
Call:
glm(formula = pass ~ exam_score, family = binomial, data = data)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -556.539 732865.023 -0.001 0.999
exam_score 8.904 11702.317 0.001 0.999
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 1.3460e+01 on 9 degrees of freedom
Residual deviance: 8.6042e-10 on 8 degrees of freedom
AIC: 4
Number of Fisher Scoring iterations: 25
>
> # Predict
> predict(log_model, [Link](exam_score=72), type="response")
>