R squared and calculation of slope, intercept and R square in R programming language.
Actuary Forums › Forums › Actuarial Subjects › CS1 (CT3) › R squared and calculation of slope, intercept and R square in R programming language.
- This topic is empty.
-
AuthorPosts
-
June 16, 2019 at 12:22 pm #23457
abhishek15512
ParticipantR squared and how to calculate slope, intercept and R square in R programming language. Once you check your conditions and you’re convinced that a linear model is appropriate for your data and is appropriate to model the relationship between your response and your explanatory variables, the next step is to check the fit of your model. In other words, how well it fits your data. And for that, we introduce a new measure called R squared.
In the previous article, I have calculated the coefficient of a linear regression equation.
https://www.linkedin.com/feed/update/urn:li:activity:6544589083459584000 𝑦̂ = 25.14146 – 13.16*x
Sxx = 3.07 – 6 * 0.650^2 = 0.535
Syy = 1839.821 – 6 * 16.5967^2 = 187.12483
Sxy = 57.694 – 6 * 0.650 * 16.5967 = -7.033
In this article, let’s see how we can calculate R squared
R squared gives you an idea of how many data points fall within the results of the line formed by the regression equation. The R squared tells us what percent of variability in the response variable is explained by the model. The remainder of the variability is explained by variables not included in the model.
R squared = Sxy^2 / Sxx* Syy
= (-7.033)^2 / 0.535 * 187.12483
= 0.4940786
This means that 49.408% of the variability in the stem length is the explained by the model.
Here are some summary notes about R squared.
R2 is the percentage of variation explained by the regression model
0 ≤ R2 ≤ 1
R2 is the sample correlation squared
And in the below screenshot, I have shown how you can calculate slope, intercept and R squared in R.
Let’s go through an example using diamond pricing data in R.
data(diamond)
y <- diamond$price x <- diamond$carat n <- length(y) beta1 <- cor(y, x) * sd(y) / sd(x) beta0 <- mean(y) - beta1 * mean(x) ssx <- sum((x - mean(x))^2) Now, let’s calculate intercept and slope using LM function in R. diamonds.lm <- lm(y ~ x , data = diamonds) Coefficients: (Intercept) x 0.0469 0.9924 Now, let’s calculate R square summary(diamonds.lm)$r.squared [1] 0.950043 Add description #actuarialscience#regression#rprogramming#statistics#CS1#CT3
-
AuthorPosts
- You must be logged in to reply to this topic.
Actuary Forums › Forums › Actuarial Subjects › CS1 (CT3) › R squared and calculation of slope, intercept and R square in R programming language.