Reputation:
First of all let me tell you I'm very new to R, so my question might sounds weird to you, I want to predict the weight of an individual from the height. For that I need to find the equation of the regression line (in the following form) for this problem:
Weight = intercept + (slope) x Height
I've the following form of data ?
Any idea about how to find regression line equation
dat <- read.table(text = "SampleNo,Height,Weight
1,65.78,112.99
2,71.52,136.49
3,69.40,153.03
4,68.22,142.34
5,67.79,144.30
6,68.70,123.30
7,69.80,141.49
8,70.01,136.46
9,67.90,112.37",
sep = ",", header = T)
Upvotes: 1
Views: 1285
Reputation: 1743
You can accomplish this with the lm
function.
lm1 <- lm(Weight ~ Height, data = dat )
I want to predict Weight
as a function of Height
so I use the syntax Weight ~ Height
.
Finally, I run the coefficients
function on the lm1
object to get the coefficient for Height
and the intercept.
coefficients(lm1)
(Intercept) Height
-177.650244 4.525168
If I want to predict the result for Weight
for a certain set of Heights
, I can do that with the following:
> predict(lm1, newdata = data.frame((Height = c(65, 68.5, 71.6))))
1 2 3
116.4857 132.3238 146.3518
You can get additional information about the fit using the summary
function on the lm1
object. This will provide R^2 values, more info on the estimates, etc...
summary(lm1)
Call:
lm(formula = Weight ~ Height, data = vals)
Residuals:
Min 1Q Median 3Q Max
-17.239 -9.500 -2.697 11.283 16.634
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -177.650 193.247 -0.919 0.389
Height 4.525 2.808 1.611 0.151
Residual standard error: 13.03 on 7 degrees of freedom
Multiple R-squared: 0.2705, Adjusted R-squared: 0.1663
F-statistic: 2.596 on 1 and 7 DF, p-value: 0.1512
A great (free) resource for regression and many other machine learning techniques in R can be found here. It's a great book to learn about stats and how to implement those methods in R.
Upvotes: 1