Search code examples
rrelationshipbayesianinference

Use bayesian inference to find relationship between variables


I have a lot of data, like the image below, which have a relationship with each other. I want to make an equation to describe this relationship, something like Power = a * WindSpeed ^ b . How can I use Bayesian Inference to find a and b? I want to use R for this. Power & Wind


Solution

  • Welcome to SO and good luck. Do not forget to pay attention to the comment (they are really relevant & you can increase probability to get an answer). Please see below example of using Bayesian univariate regression using Bolstadt package.

    library(Bolstad)
    
    # Simulation of Power vs Wind
    # Power = 5 * Windspeed ^ 2
    set.seed(123)
    n <- 100
    
    # y = Power
    # x = WindSpeed
    # e = error term
    
    x <- (1:(25 * n))/ n
    e <- rnorm(length(x)) / 10
    
    # y = a * x ^ b
    # log(y) = log(a) + b * log(x) + e  
    # or
    # in exponential form
    y <- exp(log(5) + e) * x ^ 2
    
    # bayes univariate linear regression model
    z <- bayes.lin.reg(log(y), log(x))
    # Standard deviation of residuals:  0.0943 
    # Posterior Mean Posterior Std. Deviation
    # -------------- ------------------------
    #   Intercept:  6.076          0.0059657               
    #   Slope:      1.996          0.0062209    
    

    Distribution

    # ------------------------------------------------
    # pay attention the result of bayession regression
    # are shifted for intercept by the mean
    # is is accouted as below
    intercept_shifted <- z$intercept$mean - z$slope$mean * mean(log(x))
    intercept_shifted
    # [1] 1.617218
    
    # validate by standar linear model:
    lm(log(y) ~ log(x))
    # Coefficients:
    #  (Intercept)       log(x)  
    #  1.617        1.996  
    
    a = exp(intercept_shifted) 
    a
    # [1] 5.039051
    b = z$slope$mean
    b
    # [1] 1.996134