0
$\begingroup$

I am trying to predict the variable "Mittelwert.von.BAGSOLD" in this dataset:

> str(evaluate)
 'data.frame':  38 obs. of  8 variables:
  $ Zeilenbeschriftungen  : chr  "V104000" "V113396" "V114530" "V114538" ...
  $ Mittelwert.von.YIELD  : num  58.6 70.1 61.7 61.2 62.7 ...
  $ Mittelwert.von.RM     : num  1.5 3.9 2.7 2.8 2.9 ...
  $ Mittelwert.von.BAGSOLD: num  NA NA NA NA NA NA NA NA NA NA ...
  $ CHECKYIELD            : num  0.0687 0.1443 0.1075 0.1204 0.1207 ...
  $ ELITEYIELD            : num  0.0933 0.2706 0.0739 0.0783 0.0827 ...
  $ CHECKRM               : num  0 0.1716 0.0575 0.0684 0.0907 ...
  $ ELITERM               : num  0 0.238 0.0482 0.0606 0.0874 ...

For this, I have created a model with training data:

> summary(sigfit4.1)

Call:
lm(formula = Mittelwert.von.BAGSOLD[1:33] ~ Mittelwert.von.YIELD[1:33] + 
    Mittelwert.von.RM[1:33] + I(Mittelwert.von.RM[1:33]^2) + 
    CHECKYIELD[1:33] + ELITEYIELD[1:33])

Residuals:
    Min      1Q  Median      3Q     Max 
-587705 -230267  -34595  188290 1031175 

Coefficients:
                         Estimate Std. Error t value Pr(>|t|)  
(Intercept)                    172229     811218   0.212   0.8335  
Mittelwert.von.YIELD[1:33]      20184      14071   1.435   0.1629  
Mittelwert.von.RM[1:33]       -504961     504743  -1.000   0.3260  
I(Mittelwert.von.RM[1:33]^2)    89627      97688   0.917   0.3670  
CHECKYIELD[1:33]              6084094    3384477   1.798   0.0834 .
ELITEYIELD[1:33]             -5212476    2097812  -2.485   0.0195 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 342200 on 27 degrees of freedom
Multiple R-squared:  0.4386,    Adjusted R-squared:  0.3347 
F-statistic:  4.22 on 5 and 27 DF,  p-value: 0.005798

When I use the predict function, it gives me this:

> predict(sigfit4.1, evaluate)
    1         2         3         4         5         6 
 731637.9  449129.3  976552.2 1020964.9 1029174.9 1034898.2 
    7         8         9        10        11        12 
 917621.8  864538.9 1082519.9 1097058.9  988477.0  996947.2 
   13        14        15        16        17        18 
 859176.3  806050.6 1037852.4  505977.2  867236.8  867013.6 
   19        20        21        22        23        24 
 798397.0  839654.8  991860.7  743150.7  805299.0  772278.8 
   25        26        27        28        29        30 
 746185.6  669009.0  672425.3  236245.3  532174.5  471704.5 
   31        32        33 
1106502.1  925001.8  933865.8 
Warnmeldung:
'newdata' had 38 rows but variables found have 33 rows

How do I get the prediction data for all 38 rows?

$\endgroup$

closed as off-topic by gung, Haitao Du, whuber Dec 6 '16 at 16:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Please add a reproducible example for people to work with. $\endgroup$ – gung Dec 6 '16 at 15:27
  • 2
    $\begingroup$ I'm voting to close this question as off-topic because it is about how to use R without a reproducible example. $\endgroup$ – gung Dec 6 '16 at 15:28
1
$\begingroup$

The problem you have is produced by the way you used the formula. Notice in your summary that your variables have names which ends with [1:33]. I think this is not your intention, since usually you want to use the names of the variables without indices.

Instead of that, for training time, use the specification with formula and data parameters. Like small made up example:

df <- data.frame(y=rnorm(100, 0, 2), x1=rnorm(100, 0, 10), x2=rnorm(100, 1, 1))
summary(df)

lm1 <- lm(formula = y ~ x1 + x2, data = df[1:33,])
summary(lm1)

predict(lm1, df[34:100,])

In your case it should be:

lm(formula = Mittelwert.von.BAGSOLD ~ Mittelwert.von.YIELD
+ Mittelwert.von.RM + I(Mittelwert.von.RM^2) 
+ CHECKYIELD + ELITEYIELD, data = sigfit4.1[1:33])
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.