Predicting new values from a model.
1
3
Entering edit mode
5.9 years ago

Very basic question here (so that I'm afraid to ask). Suppose I have 50 pairs of data and I want to linearly model my data and predict a single new pair to my single input. What should I do?

a= matrix (rnorm(100),50)
l= lm (a[,2]~a[,1])
predict (l, 2.1)

doesn't work.

lm predict R • 1.2k views
ADD COMMENT
3
Entering edit mode
5.9 years ago
zx8754 11k

Try this example:

# reproducible example input
set.seed(1); a <- data.frame(matrix(rnorm(100), ncol = 2))
head(a)
#           X1         X2
# 1 -0.6264538  0.3981059
# 2  0.1836433 -0.6120264
# 3 -0.8356286  0.3411197
# 4  1.5952808 -1.1293631
# 5  0.3295078  1.4330237
# 6 -0.8204684  1.9803999

# get fit
l <- lm (X2 ~ X1, data = a)

# new input data with only X1 column, because we want to predict X2
newData <- data.frame(X1 = 2.1)

newData$X2 <- predict(l, newData)
newData
#    X1         X2
# 1 2.1 0.02624987
ADD COMMENT
0
Entering edit mode

Thank you indeed very much.

ADD REPLY

Login before adding your answer.

Traffic: 2909 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6