0

I have a simple problem but can't seem to figure out what I'm doing wrong. I am using predict to estimate values from a fitted linear model. The following code works correctly:

x <- c(1, 2, 3, 4, 5 , 6)
y <- c(1, 4, 9, 16, 25, 36)
model <- lm(y ~ x)
predict(model, newdata = data.frame(x=7))

However, when I use the same data for x and y, but in a dataframe, it does not work. For example,

df2 <- structure(list(x = c(1, 2, 3, 4, 5, 6), y = c(1, 4, 9, 16, 25,36)), 
                 .Names = c("x", "y"), row.names = c(NA, -6L), class = "data.frame")
model <- lm(df2$y ~ df2$x)
predict(model, newdata = data.frame(x=7))

throws the error:

Warning message:
'newdata' had 1 row but variables found have 6 rows 

I am using the same exact data and am expecting the same answer. Can anyone tell me what I am doing wrong? Thanks!

Thomas
  • 2,484
  • 8
  • 30
  • 49

1 Answers1

2

Try

model = lm(y ~ x, data = df2)
predict(model, newdata = data.frame(x = 7))

The problem is the way you specified the formula.

Fernando
  • 7,785
  • 6
  • 49
  • 81
  • Thanks Fernando, worked perfectly! Now I will try with my real data! – Thomas Apr 17 '14 at 01:32
  • 1
    It turned out that my issue resulted from using colnames to rename the dataframe columns before fitting lm. the df2$y and df$x errors were introduced somewhere during my attempts to solve that underlying issue. At any rate, the problem is now solved and I thank you again very much for your help Fernando. – Thomas Apr 17 '14 at 02:10