Question: Predicting new values from a model.
3
gravatar for english.server
9 months ago by
Germany
english.server140 wrote:

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 • 258 views
ADD COMMENTlink modified 9 months ago • written 9 months ago by english.server140
3
gravatar for zx8754
9 months ago by
zx87546.8k
London
zx87546.8k wrote:

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 COMMENTlink written 9 months ago by zx87546.8k

Thank you indeed very much.

ADD REPLYlink written 9 months ago by english.server140
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1883 users visited in the last hour