+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 16

Thread: confidence interval with R

  1. #1
    Points: 427, Level: 8
    Level completed: 54%, Points required for next Level: 23

    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    confidence interval with R



    I am trying to predict the amount that a male with average status, income and verbal score would spend along with an appropriate 95% CI.

    I used my linear model with all my variables and sex is coded as male=0 and female=1 in data set.
    I think I did something wrong because I get all 47 observations but there are 19 females and 28 males.
    I set up the prediction as:
    > predict(mdl, sex=0, interval='confidence', level=0.90)
    fit lwr upr
    1 -10.6507430 -21.4372267 0.1357407
    2 -9.3711318 -21.9428731 3.2006095
    3 -5.4630298 -15.0782882 4.1522286
    4 24.7957487 12.5630143 37.0284831...
    please help...

  2. #2
    RotParaTon
    Points: 46,151, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,064
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: confidence interval with R

    It's not clear what you're trying to do. Here is an example of building a model and using predict:

    Code: 
    
    n <- 30
    x1 <- rnorm(n)
    x2 <- runif(n)
    y <- 2 + x1 - .5*x2 + rnorm(n)
    dat <- data.frame(y, x1, x2)
    
    o <- lm(y ~ x1 + x2, data = dat)
    
    # Gives predictions for every value in the data set
    predict(o, interval = "confidence", level = .90)
    
    # If we want new predictions we need to use the newdata
    # argument
    
    newdat <- data.frame(x1 = c(1, 2), x2 = c(.5, .7))
    predict(o, newdata = newdat, interval = "confidence", level = .9)
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  3. #3
    Points: 427, Level: 8
    Level completed: 54%, Points required for next Level: 23

    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: confidence interval with R

    I have a data set with 47 observation and 5 variables (sex, income, status, verbal score and spending) where sex is coded as 0=male and female for 1.
    The questions was to predict the amount that a male with average income, status and verbal score would spend with a 95%CI.
    i know i have to use my linear model to predict average, but my prediction set up is wrong. How do I predict for male when its coded as such in my data...that is where i get confused...

  4. #4
    RotParaTon
    Points: 46,151, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,064
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: confidence interval with R

    You would use the exact same setup as I gave in my previous post but you would plug in your variables and the values you want them to take into the newdata parameter. In your case sex=0 gives male so you would make sure that the sex portion of newdata had a 0...
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  5. #5
    Points: 427, Level: 8
    Level completed: 54%, Points required for next Level: 23

    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: confidence interval with R

    sorry Dason but I like I said I am new to R and I still dont understand. I am trying to clarify the prediction and CI for the problem because I have to then repeat the prediction with maximal values and compare CI to determine which is wider. Thanks

  6. #6
    Points: 427, Level: 8
    Level completed: 54%, Points required for next Level: 23

    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: confidence interval with R

    kindly explain in more detail so i can try to complete the problem. Thank you

  7. #7
    RotParaTon
    Points: 46,151, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,064
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: confidence interval with R

    I guess it's still not clear to me what is giving you trouble. Have you worked through that example I gave you?
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  8. #8
    Points: 427, Level: 8
    Level completed: 54%, Points required for next Level: 23

    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: confidence interval with R

    I need the prediction for male but i get all 47 observation instead of the prediction for male on average?

    then when doing the newdata as you suggested > I used the max. values found in my summary for each variable ...Was that correct?
    g2<-data.frame(status=75, income=15, verbal=10, sex=0)
    > predict(g,g2, level=.90)
    > predict(g,g2, interval='confidence', level=.90)
    fit lwr upr
    1 71.30794 47.07516 95.54072

  9. #9
    RotParaTon
    Points: 46,151, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,064
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: confidence interval with R

    So to get the predictions for the males where everything else is average put in the average values for the other predictors.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  10. #10
    Points: 427, Level: 8
    Level completed: 54%, Points required for next Level: 23

    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: confidence interval with R

    but I also need the "prediction" band for max values as well correct?
    > predict(g,g2, interval='prediction', level=.90)
    fit lwr upr
    1 71.30794 26.10037 116.5155

  11. #11
    RotParaTon
    Points: 46,151, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,064
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: confidence interval with R

    Ok... I don't know what to do with this thread anymore. You aren't actually asking a question there. It's not clear what is giving you problems.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  12. #12
    Test of Gnomality
    Points: 8,264, Level: 61
    Level completed: 38%, Points required for next Level: 186
    Awards:
    Activity Award
    hlsmith's Avatar
    Posts
    1,504
    Thanks
    98
    Thanked 255 Times in 248 Posts

    Re: confidence interval with R

    If it is still unclear, I believe Snowy88 wants 95% confidence interval around the sex estimate (beta coefficient).

  13. #13
    RotParaTon
    Points: 46,151, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardFrequent PosterCommunity AwardMaster Tagger
    Dason's Avatar
    Location
    Ames, IA
    Posts
    9,064
    Thanks
    211
    Thanked 1,605 Times in 1,375 Posts

    Re: confidence interval with R

    Code: 
    confint(o)
    will do that. But I don't think that's what they want. They talk about prediction and this doesn't get you a prediction.
    "His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich

  14. #14
    Test of Gnomality
    Points: 8,264, Level: 61
    Level completed: 38%, Points required for next Level: 186
    Awards:
    Activity Award
    hlsmith's Avatar
    Posts
    1,504
    Thanks
    98
    Thanked 255 Times in 248 Posts

    Re: confidence interval with R

    I was assuming this from their posts yesterday. Another way not using R would be:

    If sample size > 30 the following is typically used:
    -22.1133-1.96(8.21111) and -22.1133+1.96(8.21111)
    Or (95% CI: -38.20708, -6.01952)

  15. #15
    Points: 427, Level: 8
    Level completed: 54%, Points required for next Level: 23

    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: confidence interval with R


    I need the prediction on average for male with a 95%CI then repeat with maximal values using the variables status, income and verbal. After I need to determine which CI is wider and explain...Now If I did the predictions correctly shown above with the mean then the max values. But doesn't that give 2 prediction intervals and 2 confident intervals for both the average and max. values ie..Average
    > g1<-data.frame(status=43, income=4.64, verbal=6.66, sex=0)
    > predict(g,g1, interval='confidence', level=.90)
    fit lwr upr
    1 28.11506 19.7603 36.46983
    > predict(g,g1, interval='prediction', level=.90)
    fit lwr upr
    1 28.11506 -10.95281 67.18293

    How will I create a plot to show which CI are wider?

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts








Advertise on Talk Stats