R Query Chp 12 - Linear Regression

Discussion in 'CS1' started by Naitik Shah, Sep 9, 2023.

  1. Naitik Shah

    Naitik Shah Keen member

    Hi,

    In Exercise 12.06 of PBOR where baby.weight is being used in Linear Regression, the code provided by ActEd & my code seems to be similar but I am not getting the answer for the same.

    Code by ActEd:
    baby <- read.table("baby weights.txt",header=TRUE)
    baby
    attach(baby)
    model1 <- lm(weight~gestation)
    coef(model1)[1]+coef(model1)[2]*33
    #2.14kg
    #Wrap the "gestation" parameter inside a data frame
    newdata1 <-data.frame(gestation=33)
    #Then we use the "predict" function
    predict(model1,newdata1)
    #2.14kg


    My Code:
    model1 <- lm(baby.weights$weight~baby.weights$gestation)
    coef(model1)[1]+coef(model1)[2]*33
    ## 2.141429
    newdata1 <- data.frame(baby.weights$gestation = 33)
    The error in the above code: unexpected '=' in "newdata1 <- data.frame(baby.weights$gestation="

    Now I tried the method of attaching the headers:
    attach(baby.weights)
    newdata1 <- data.frame(gestation=33)
    predict(model1,newdata1)
    ## 1 2 3 4 5 6
    ## 1.528571 1.937143 2.345714 2.754286 3.162857 3.571429
    Warning message:
    'newdata' had 1 row but variables found have 6 rows


    In summary, the issue that I have been encountering is that despite having the same code as that provided by ActEd, I am not getting the answer that I should be getting i.e. in this case 2.141429.

    Request someone to help me out here.

    Regards,
     
  2. Naitik Shah

    Naitik Shah Keen member

    Hi,
    So my query is basically that I am not getting a single value like the one which is being provided in the solutions but I am getting 6 values instead.

    Best,
    Naitik Shah.
     
  3. John Lee

    John Lee ActEd Tutor Staff Member

    The issue is the name you are using in the data.frame does not match the name used in the lm function.
    Hence, the predict function has no idea what to do.

    However, if you try to match it using:
    newdata1 <- data.frame(baby.weights$gestation=33)
    you'll still get a problem as $'s don't work in a data.frame and you end up with the same problem.

    So either use:
    weight <- baby.weights$weight
    gestation <- baby.weights$gestation
    lm(weight~gestation)

    or

    lm(weight~gestation, data=baby.weights)
    predict(model1,newdata1)
    or
    attach(baby.weights)
    lm(weight~gestation)

    Then all of these will work with
    newdata1 <- data.frame(gestation=33)
     
  4. Naitik Shah

    Naitik Shah Keen member

    Hi John,

    Thanks a lot for your prompt response. The issue has been resolved.

    Regards.
     
    John Lee likes this.

Share This Page