R - Program

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Ex- 02

Correlation Test
Problem: The following data gives the marks obtained by 12 students
in statistics and mathematics:

Students 1 2 3 4 5 6 7 8 9 10 11 12
Statistics 2 3 3 5 6 9 14 15 19 21 22 23
Mark s mathematics 23 24 24 23 17 28 38 34 35 39 41 43

Compute the coefficient of correlation by the method of concurrent deviations.


Aim: Model fitting and investigating relationships between two variables
within a correlation framework.
R function to compute Correlation
To determine if the correlation coefficient between two variables is
statistically significant, you can perform a correlation test in R using the
following syntax:
cor.test(x, y, method=c(“pearson”, “kendall”, “spearman”))
• x, y: Numeric vectors of data.
• method: Method used to calculate correlation between two vectors.
Default is “pearson.”
Algorithm:
 Enter the values in the given data.
 Define the suitable R-coding for the given data.
 Interpretation of results.
INPUT
 # Data in two numeric vectors
 statistics= c(2, 3, 3, 5, 6, 9, 14, 15, 19, 21, 22, 23)
 mathematics= c(23, 24, 24, 23, 17, 28, 38, 34, 35, 39, 41, 43)
 #create scatterplot
 plot(statistics, mathematics, pch=16)

 # Compute correlation test


 cor.test(statistics,mathematics)
OUTPUT:
 plot(statistics, mathematics, pch=16)

#perform correlation test between the two vectors

cor.test(statistics,mathematics)

Pearson's product-moment correlation

data: x and y
t = 7.8756, df = 10, p-value = 1.35e-05
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.7575203 0.9799783
sample estimates:
cor
0.9279869

Interpretation:-
 The correlation coefficient between the two vectors turns out to be
0.9279869.
 The test statistic turns out to be 7.8756 and the corresponding p-value is
1.35e-05.
 Since this value is less than .05, we have sufficient evidence to say that
the correlation between the two variables is statistically significant.
EX-03
Linear Regression Model
Problem:-
Obtain a linear relationship between weight (kg) and height (cm) of 10 subjects.

Height 151 174 138 186 128 136 179 163 152 131
Weight 63 81 56 91 47 57 76 72 62 48

Aim: Model fitting and investigating relationships between two variables


within a simple regression framework.
R function to compute linear regression
The basic syntax for lm() function in linear regression is
 lm(formula,data)

 formula is a symbol presenting the relation between x and y.


 data is the vector on which the formula will be applied.
Algorithm:
 Enter the values in the given data.
 Define the suitable R-coding for the given data.
 Interpretation of results.
INPUT

 # Create the predictor and response variable.


 height <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
 weight <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
 #fit simple linear regression model
 # Apply the lm() function.
 relation <- lm(weight ~ height)
 #view model summary
 print(summary(relation))
 # Find weight of a person with height 170.
 a <- data.frame(height = 170)
 result <- predict(relation,a)
 print(result)
 # Plot the chart.
 plot(weight, height, col = "blue",main = "Height & Weight Regression",
 abline(lm(height ~ weight)),cex = 1.3,pch = 16,xlab = "Weight in Kg",ylab = "Height in cm")
OUTPUT:
print(summary(relation))
Call:
lm(formula = weight ~ height)

Residuals:
Min 1Q Median 3Q Max
-6.3002 -1.6629 0.0412 1.8944 3.9775

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -38.45509 8.04901 -4.778 0.00139 **
x 0.67461 0.05191 12.997 1.16e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.253 on 8 degrees of freedom


Multiple R-squared: 0.9548, Adjusted R-squared: 0.9491
F-statistic: 168.9 on 1 and 8 DF, p-value: 1.164e-06

#Predict the weight of new persons


print(result)
1
76.22869
# Plot the chart.

Interpretation:-
 The fitted regression equation is:

weight =-38.45509+0.67461*Height
weight =-38.45509+0.67461*(170)
weight =76.22
 This is the p-value associated with the model coefficients. Since the p-
value for Height (1.164e-06) is significantly less than .05, we can say that
there is a statistically significant association between Height and weight.
 The F-statistic (168.9) and the corresponding p-value (1.164e-06) tell us
the overall significance of the regression model.

You might also like