# How to predict a value using r [closed]

Say I have a dataset with 2 columns, one for height and one for weight of a person. How would I predict the weight of a person upon receiving his height?

• @user777 I was going to make a comment suggesting that the relationship between weight and height would tend to increase more quickly than linearly and that a simple linear regression model relating the two would probably not be adequate. Dec 23, 2015 at 16:25

One example of a very basic linear model prediction would be:

#two vectors of height and weight, totally fabricated data here
height.inches <- c(60, 62, 64, 70, 65, 68)
weight.pounds <- c(140, 160, 150, 240, 220, 195)

#create a data frame from the two vectors
ds <- data.frame(cbind(height.inches,weight.pounds))

#create a simple linear regression model
ds.lm <- lm(weight.pounds~height.inches, data=ds)

#review the model -- not getting into it here, but this will provide you
#important information on the validity/applicability of your model

summary(ds.lm)

#using the model, predict the weight of a person based on an input height
#since the linear model is very simple, the easiest way to predict is to use the parameter estimates from the model

input.height <- 75

weight.prediction <- as.numeric((ds.lm$coefficients["height.inches"] * input.height) + ds.lm$coefficients[1])

weight.prediction


As with any statistical prediction of any kind, you will want to be very wary of the results of this prediction, as it will only represent the relationship presented in the data you give it and the limitations and assumptions of the model you use.

Some questions (among many, many possibly beneficial questions) to consider when choosing an approach:

What do I want to use the prediction for?

How accurate does the prediction need to be?

Does the prediction need to be generalizable to all humans, or only to a specific group?

If needed, is there any additional information I could use in order to improve the model? (Gender/Ethnicity/Bone Structure/Affinity for Cake etc.)

Is the relationship between my variables linear, non-linear, logarithmic, or completely unknown? Do I have to take an initial step to determine this relationship?

The simplicity of the question you're asking implies that perhaps you haven't thought through the answers to any of these questions (or at least haven't presented the answers to us to help you).

If you do come up with answers to these questions (or any others), it will help you better define what exactly you are trying to do with this prediction and what the prediction is, which will in turn help you to ask questions (whether to Google, this site, books you may read, or even to yourself) that will be productive in helping you.

you first need to fit a model. for example, a linear model

# y is the outcome, x is the predictor
myfit = lm(y ~ x)


Then you can make predictions with the predict function

predict(myfit, newdata=data.frame(x=3))


gives a prediction when x=3. Different models will produce different predictions so make sure the fitted model makes sense before you start interpreting the predicted values.

• You might want to look at our editing help to see how to format your code better. Dec 23, 2015 at 18:11