Asked  7 Months ago    Answers:  5   Viewed   27 times

I posted earlier today about an error I was getting with using the predict function. I was able to get that corrected, and thought I was on the right path.

I have a number of observations (actuals) and I have a few data points that I want to extrapolate or predict. I used lm to create a model, then I tried to use predict with the actual value that will serve as the predictor input.

This code is all repeated from my previous post, but here it is:

df <- read.table(text = '
     Quarter Coupon      Total
1   "Dec 06"  25027.072  132450574
2   "Dec 07"  76386.820  194154767
3   "Dec 08"  79622.147  221571135
4   "Dec 09"  74114.416  205880072
5   "Dec 10"  70993.058  188666980
6   "Jun 06"  12048.162  139137919
7   "Jun 07"  46889.369  165276325
8   "Jun 08"  84732.537  207074374
9   "Jun 09"  83240.084  221945162
10  "Jun 10"  81970.143  236954249
11  "Mar 06"   3451.248  116811392
12  "Mar 07"  34201.197  155190418
13  "Mar 08"  73232.900  212492488
14  "Mar 09"  70644.948  203663201
15  "Mar 10"  72314.945  203427892
16  "Mar 11"  88708.663  214061240
17  "Sep 06"  15027.252  121285335
18  "Sep 07"  60228.793  195428991
19  "Sep 08"  85507.062  257651399
20  "Sep 09"  77763.365  215048147
21  "Sep 10"  62259.691  168862119', header=TRUE)

str(df)
'data.frame':   21 obs. of  3 variables:
 $ Quarter   : Factor w/ 24 levels "Dec 06","Dec 07",..: 1 2 3 4 5 7 8 9 10 11 ...
 $ Coupon: num  25027 76387 79622 74114 70993 ...
 $ Total: num  132450574 194154767 221571135 205880072 188666980 ...

Code:

model <- lm(df$Total ~ df$Coupon, data=df)

> model

Call:
lm(formula = df$Total ~ df$Coupon)

Coefficients:
(Intercept)    df$Coupon  
  107286259         1349 

Predict code (based on previous help):

(These are the predictor values I want to use to get the predicted value)

Quarter = c("Jun 11", "Sep 11", "Dec 11")
Total = c(79037022, 83100656, 104299800)
Coupon = data.frame(Quarter, Total)

Coupon$estimate <- predict(model, newdate = Coupon$Total)

Now, when I run that, I get this error message:

Error in `$<-.data.frame`(`*tmp*`, "estimate", value = c(60980.3823396919,  : 
  replacement has 21 rows, data has 3

My original data frame that I used to build the model had 21 observations in it. I am now trying to predict 3 values based on the model.

I either don't truly understand this function, or have an error in my code.

Help would be appreciated.

Thanks

 Answers

71

First, you want to use

model <- lm(Total ~ Coupon, data=df)

not model <-lm(df$Total ~ df$Coupon, data=df).

Second, by saying lm(Total ~ Coupon), you are fitting a model that uses Total as the response variable, with Coupon as the predictor. That is, your model is of the form Total = a + b*Coupon, with a and b the coefficients to be estimated. Note that the response goes on the left side of the ~, and the predictor(s) on the right.

Because of this, when you ask R to give you predicted values for the model, you have to provide a set of new predictor values, ie new values of Coupon, not Total.

Third, judging by your specification of newdata, it looks like you're actually after a model to fit Coupon as a function of Total, not the other way around. To do this:

model <- lm(Coupon ~ Total, data=df)
new.df <- data.frame(Total=c(79037022, 83100656, 104299800))
predict(model, new.df)
Tuesday, June 1, 2021
 
mozlima
answered 7 Months ago
42

There are several problems here:

  1. The newdata argument of predict() needs a predictor variable. You should thus pass it values for Coupon, instead of Total, which is the response variable in your model.

  2. The predictor variable needs to be passed in as a named column in a data frame, so that predict() knows what the numbers its been handed represent. (The need for this becomes clear when you consider more complicated models, having more than one predictor variable).

  3. For this to work, your original call should pass df in through the data argument, rather than using it directly in your formula. (This way, the name of the column in newdata will be able to match the name on the RHS of the formula).

With those changes incorporated, this will work:

model <- lm(Total ~ Coupon, data=df)
new <- data.frame(Coupon = df$Coupon)
predict(model, newdata = new, interval="confidence")
Wednesday, July 28, 2021
 
sholsinger
answered 5 Months ago
53

Since this is a typical problem in chemistry (predict values from a calibration), package chemCal provides inverse.predict. However, this function is limited to "univariate model object[s] of class lm or rlm with model formula y ~ x or y ~ x - 1."

x <- c(0, 40, 80, 120, 160, 200)
y <- c(6.52, 5.10, 4.43, 3.99, 3.75, 3.60)
plot(x,y)
model <- lm(y ~ x)
abline(model)
require(chemCal)
ynew <- c(5.5, 4.5, 3.5)
xpred<-t(sapply(ynew,function(y) inverse.predict(model,y)[1:2]))
#  Prediction Standard Error
#[1,] 31.43007   -38.97289     
#[2,] 104.7669   -36.45131     
#[3,] 178.1037   -39.69539
points(xpred[,1],ynew,col="red")

Warning: This function is quite slow and not suitable, if you need to inverse.predict a large number of values.

If I remember correctly, the neg. SEs occur because the function expects the slope to be always positive. Absolute values of SE should still be correct.

Wednesday, August 11, 2021
 
K. Gl.
answered 4 Months ago
66

As per Firebase Cloud Messaging documentation-If Activity is in foreground then onMessageReceived will get called. If Activity is in background or closed then notification message is shown in the notification center for app launcher activity. For More information Check this link

Thursday, November 4, 2021
 
VostanAzatyan
answered 1 Month ago
32

You can read it like this:

eval (Div x y) = eval x >>= (n ->
    eval y >>= (m ->
        safediv n m))

when you want do eval (Div x y)then

  • first eval x:
    • if was Just n (using the first >>=)
    • then take the n and have a look at eval y (using the first >>=)
      • if the last is Just m (second >>=)
      • then take the m and do a (second >>=)
      • savediv n m to return it's result - you still have the n from your closure!.

in ever other caes return Nothing

So here the (>>=) just helps you to deconstruct.

Maybe it's easier to read and understand in the do form:

eval (Val n) = Just n
eval (Div x y) = do
    n <- eval x
    m <- eval y
    safediv n m

which is just syntactic sugar around (>>=)

let's chase the cases:

1. eval x = Nothing and eval y = Nothing:
eval x >>= (...) = Nothing >>= (...) = Nothing
2. eval x = Nothing and eval y = Just n:

which is just the same:

eval x >>= (...) = Nothing >>= (...) = Nothing
3. eval x = Just n and eval y = Nothing:
eval x >>= (n -> eval y >>= (...))
= Just n >>= (n -> eval y >>= (...)) 
= Just n >>= (n -> Nothing)
= Nothing
4. eval x = Just n and eval y = Just m:
eval x >>= (n -> Just m >>= (...))
= Just n >>= (n -> Just m >>= (...)) 
= Just n >>= (n -> Just m >>= (m -> safediv n m))
= (first >>= for Just) = Just m >>= (n -> safediv n m)
= (second >>= for Just) = safediv n m
Monday, November 29, 2021
 
bsd
answered 1 Week ago
bsd
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share