4. Performing Statistics with R- I
4A) write an R program to apply built-in statistical function [hint: mean, median, standard deviation and others].
CODE:
# Sample data set
data <- c(15, 20, 25, 30, 35, 40, 45, 50)
# Calculate mean
mean_value <- mean(data)
cat("Mean:", mean_value, "\n")
# Calculate median
median_value <- median(data)
cat("Median:", median_value, "\n")
# Calculate standard deviation
std_dev <- sd(data)
cat("Standard Deviation:", std_dev, "\n")
OUTPUT :
Mean: 32.5
Median:32.5
Standard Deviation:12.24745
4B) Write an R program to demonstrate linear and multiple analysis.
CODE:
#To calculate the best fit line for height and weight of a person
human.data=data.frame(
height=c(5.1,5.5,5.8,6.1,6.4,6.7,6.4,6.1,5.10,5.7),
weight=c(63,66,69,72,75,78,75,72,69,66))
human.data
plot(human.data$height,human.data$weight)
simple.regression=lm(weight~height,data=human.data)
summary(simple.regression)
abline(simple.regression,col="Red",lwd=2)
OUTPUT :
4B) W2 EXAMPLE
CODE:
#to predict the weight based on given height
#height of person
x=c(5.1,5.5,5.8,6.1,6.4,6.7,6.4,6.1,5.10,5.7)
#weight of person
y=c(63,66,69,72,75,78,75,72,69,66)
#apply the linear model function
relation=lm(y~x)
summary(relation)
#find the weight of the person wth height 7 ft
a= data.frame(x=7)
result=predict(relation,a)
print(result)
OUTPUT :
1
79.16611

.jpeg)
