Search code examples
rregressionpolynomialsstatistical-test

How do I write a polynomial function with my independent variable log transformed given the coefficients?


Forewarning: I am a complete noob, so I'm sorry for the dumb question. I've tried everything trying to figure out how to write out the actual polynomial function given these coefficients.

    Coefficients:
                             Estimate Std. Error t value Pr(>|t|)
(Intercept)                   89.6131     0.8525 105.119  < 2e-16
poly(log(x), 3, raw = TRUE)1 -36.8351     2.3636 -15.584 1.13e-10
poly(log(x), 3, raw = TRUE)2   6.9735     1.6968   4.110 0.000928
poly(log(x), 3, raw = TRUE)3  -0.7105     0.3124  -2.274 0.038063

I thought that it would just be f(x) = 89.6131 - 36.8351log(x) + 6.9735log(x^2) - 0.7105*log(x^3).

I've tried a bunch of variations of this but nothing seems to work. I'm trying to plug my polynomial function and my x-values in to Desmos and get it to return what I'm getting in R which is:

        1         2         3         4         5         6 
 9.806469 15.028672 20.317227 25.669588 28.757896 35.816853 
        7         8         9        10        11        12 
41.334623 43.919057 49.267966 53.880519 60.862101 63.830004 
       13        14        15        16        17        18 
70.390727 79.412081 80.416065 85.214063 86.165068 98.187744 
       19 
96.723278 

My x values are:

x = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)

Modeling code:

#data
x = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
y = c(10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)


#fitting the model
model1 <- lm(y~poly(log(x),3,raw=TRUE))

new.distance <- data.frame(
  distance = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.83,0.8)
)

predict(model1, newdata = new.distance)
summary(model1)

Solution

  • Libraries

    library(tidyverse)
    

    Sample data

    x <- c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.8,0.83)
    y <- c(10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100)
    
    df <-
      tibble(
        x = x,
        y = y
      ) %>% 
      mutate(
        lx = log(x)
      )
    

    Fitting model

    model1 <- lm(y~poly(log(x),3,raw=TRUE))
    

    Predicting data

    df_to_pred <-
      data.frame(
      x_to_pred = c(49.64,34.61,23.76,16.31,13.23,8.47,6.19,5.4,4.15,3.37,2.53,2.26,1.79,1.34,1.3,1.13,1.1,0.83,0.8)
    )
    

    Original data x predicted

    enter image description here

    Predicted data - function x manual

    df %>% 
      cbind(y_pred_model = predict(model1, newdata = new.distance)) %>% 
      mutate(y_pred_manual = 89.6131 - 36.8351*log(x) + 6.9735*log(x)^2 - 0.7105*log(x)^3) %>% 
      ggplot(aes(y_pred_manual,y_pred_model))+
      geom_abline(intercept = 0,slope = 1,size = 1, col = "red")+
      geom_point()
    

    enter image description here