0% found this document useful (1 vote)
29 views5 pages

Correlation and Regression Analysis in R

The document describes performing a correlation test and linear regression analysis in R on two datasets. For the first dataset, the correlation test finds a statistically significant correlation coefficient of 0.9279869 between marks in statistics and mathematics for 12 students. For the second dataset, a linear regression model fits a linear relationship between weight and height of 10 subjects. The model summary shows height is a statistically significant predictor of weight with an R-squared of 0.9548. The model is used to predict the weight of a person with a height of 170cm.

Uploaded by

delvishjohn0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
29 views5 pages

Correlation and Regression Analysis in R

The document describes performing a correlation test and linear regression analysis in R on two datasets. For the first dataset, the correlation test finds a statistically significant correlation coefficient of 0.9279869 between marks in statistics and mathematics for 12 students. For the second dataset, a linear regression model fits a linear relationship between weight and height of 10 subjects. The model summary shows height is a statistically significant predictor of weight with an R-squared of 0.9548. The model is used to predict the weight of a person with a height of 170cm.

Uploaded by

delvishjohn0
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

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:
[Link](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


 [Link](statistics,mathematics)
OUTPUT:
 plot(statistics, mathematics, pch=16)

#perform correlation test between the two vectors

[Link](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 <- [Link](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.

Common questions

Powered by AI

Interpreting the significance codes for coefficients in the lm() function in R is important because these codes provide an indication of how statistically significant the coefficients are for explaining the response variable. Significance codes like '***', '**', and '*' denote the p-value categories that help assess the strength of evidence against the null hypothesis that a coefficient is zero. In the example provided, the p-value for height was 1.16e-06, denoted by '***', indicating that height is a highly significant predictor of weight in the model .

The 'Multiple R-squared' value of 0.9548 indicates that about 95.48% of the variability in the response variable (weight) can be explained by the predictor variable (height) in the model. This high R-squared value suggests a very good fit of the regression model to the data, implying that height is a strong determinant of weight in this data set .

The residual standard error of 3.253 is an estimate of the standard deviation of the residuals (differences between observed and predicted values) in the regression model. It provides a measure of the typical distance that the data points fall from the regression line. A smaller residual standard error suggests a closer fit of the model to the data. In this context, 3.253 indicates that most of the observed data points lie within 3.253 units of the predicted values based on the regression line .

A p-value of 1.35e-05 in a correlation test indicates that the probability of observing a correlation as extreme as, or more extreme than, the observed correlation under the null hypothesis (that the true correlation is zero) is very low. This strongly suggests that the observed correlation is statistically significant, and we can reject the null hypothesis with high confidence .

The intercept term of -38.45509 in the regression model suggests the predicted weight when the height is zero. In practical terms, this is a theoretical value that doesn't have a real-world interpretation since height cannot be zero. Instead, it acts as an anchor for the linear regression equation, allowing the line to be fitted optimally to the data by adjusting to average trends .

The F-statistic of 168.9 in regression analysis measures the overall significance of the regression model. A high F-statistic, coupled with a very low p-value (1.164e-06), indicates that the regression model provides a good fit for the data and that at least one predictor variable (in this case, height) is significantly related to the response variable (weight). This suggests that the model significantly explains the variability in the response data .

To determine the statistical significance of a correlation coefficient between two variables in R, you can perform a correlation test using the function cor.test(x, y, method=c(“pearson”, “kendall”, “spearman”)). This function calculates the correlation coefficient and tests its significance against the null hypothesis that the true correlation is zero. For instance, using the Pearson method with vectors corresponding to marks in statistics and mathematics, the test statistic was 7.8756 and the p-value was 1.35e-05. Since this p-value is less than 0.05, the correlation is statistically significant .

A correlation coefficient of 0.9279869 indicates a very strong positive linear relationship between the two data sets under consideration. This suggests that as one variable increases, the other variable also tends to increase proportionately. The correlation is quite close to 1, implying a strong degree of association .

The linear regression equation weight = -38.45509 + 0.67461 * Height represents the predicted relationship between weight and height. For each unit increase in height, weight increases by 0.67461 units, assuming a linear relationship. The intercept -38.45509 represents the estimated weight when height is zero, which is a theoretical value. This model provides a mathematical way to predict weight based on height .

The 95 percent confidence interval in correlation analysis gives a range of values within which the true correlation coefficient is likely to lie, with 95% confidence. For example, the interval from 0.7575203 to 0.9799783 indicates that while the sample correlation coefficient was 0.9279869, the true correlation coefficient in the population would fall within this range 95% of the time if the study were repeated. This interval provides a measure of the precision of the estimate and supports the robustness of the statistical significance associated with the observed data .

You might also like