predict function don't want to "predict" values - just returns them

Hi, I have problem, I have a values I would like to draw, bot to make a plot I'm using predict() function to jet enought data to draw a line, bot I got error and I don't now why and what to do.
My code looks like this:

results = read.csv("results.csv")
avg_results = aggregate(time ~ size:type, data=results, FUN=mean)
avg_results$std = aggregate(time ~ size:type, data=results, FUN=sd)$time

fit0 = lm(subset(avg_results$time,avg_results$type == 0) ~ poly(subset(avg_results$size,avg_results$type == 0), 3, raw=TRUE)

x = seq(0, 900, length.out = 100)
approx_fun0 = data.frame(x)
approx_fun0$y = predict(fit0, approx_fun0)

And after running last line I've got:

Error in $<-.data.frame(*tmp*, y, value = c(1 = -0.311488153801186, :
replacement has 18 rows, data has 100

Error. I can just add, that subset(avg_results$time,avg_results$type == 0) has 18 rows, and fit0 equals:

Call:
lm(formula = subset(avg_results$time, avg_results$type == 0) ~ poly(subset(avg_results$size, avg_results$type == 0), 3,
raw = TRUE))

Coefficients:>
(Intercept) poly(subset(avg_results$size, avg_results$type == >0), 3, raw = TRUE)1
-1.675e+00 1.878e-02
poly(subset(avg_results$size, avg_results$type == 0), 3, raw = TRUE)2 >poly(subset(avg_results$size, avg_results$type == 0), 3, raw = TRUE)3
-5.650e-05 5.070e-08

I would be very grateful if someone could tell me why precict() don't want to predict values, when has new x's and fit funtion (that looks like on the example in internet)

Let's try to clean this up a bit and see if you still get the error.

results = read.csv("results.csv")
avg_results = aggregate(time ~ size:type, data=results, FUN=mean)

avg_results_0 = subset(avg_results, type == 0)

fit0 = lm(time ~ poly(size, 3, row = TRUE), data = avg_results_0)

x = seq(0, 900, length.out = 100)
approx_fun0 = data.frame(size = x)
approx_fun0$y = predict(fit0, approx_fun0)

Does that work?

If anuone have the same problem - thing that helped wat to change names of variables, so they don't have number on the end, for example: approx0_fun instead of approx_fun0

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.