In [1]:
:ext QuasiQuotes
import qualified H.Prelude as H
H.initialize H.defaultConfig
[r| require("ggplot2")
    require("pracma")|]
Loading required package: ggplot2
Loading required package: pracma
0x00007fa44c00b3b0

Haskell / R Logistics First

Testing R Globals .. Must use <<- for assignment

In [2]:
[r| lm_eqn <<- function(df){
     m <- lm(actualA ~ predictionA, df);
     eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
         list(a = format(unname(coef(m)[1]), digits = 2),
              b = format(unname(coef(m)[2]), digits = 2),
             r2 = format(summary(m)$r.squared, digits = 3)))
     as.character(as.expression(eq));
} |]
0x00007fa44ccc6f78
In [3]:
[r| 
  ggplotRegression <<- function(the.title, dat, xvar, yvar){
  
  require(ggplot2)
  
  fit <- lm(yvar~xvar, dat)
  
  ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1])) + 
    geom_point(size=2, shape=21) +
    ggtitle(the.title) +
    theme(plot.title = element_text(hjust = 0.5)) +
    theme(plot.caption = element_text(hjust = 0.5)) +
    theme(text=element_text(family="Ariel", size=12)) +
    stat_smooth(method = "lm", col = "red", se=FALSE) +
    labs( caption = paste("RSq = ",signif(summary(fit)$r.squared, 5),
                       "Intercept =",signif(fit$coef[[1]],5 ),
                       " Slope =",signif(fit$coef[[2]], 5),
                       " P =",signif(summary(fit)$coef[2,4], 5)))
} |]
0x00007fa44e7918a0

Inverse Function Solution (Try #8)

Here's the Function ... Find the Inverse

$ A = \cfrac{\theta}{\sqrt{2 - 2 \cdot cos(\theta)}} $

In [4]:
[r|
     fjay <<- function (theta) {
       return ( theta / sqrt(2 - 2*cos (theta)))
       } |]
0x00007fa44e78ef60

Let's just plot it to see what it looks like ..

In [5]:
[rgraph|
  curve(expr=fjay, from = 0.0, to = 3.14, family = "arial")
  |]
In [6]:
[rprint|jaydata <<- read.table ("fromjay02.csv", header=TRUE, sep=",");
     |]
   Theta        A
1   0.00 1.000000
2   0.05 1.000104
3   0.10 1.000417
4   0.15 1.000938
5   0.20 1.001669
6   0.25 1.002609
7   0.30 1.003760
8   0.35 1.005122
9   0.40 1.006698
10  0.45 1.008488
11  0.50 1.010493
12  0.55 1.012716
13  0.60 1.015159
14  0.65 1.017824
15  0.70 1.020712
16  0.75 1.023828
17  0.80 1.027173
18  0.85 1.030751
19  0.90 1.034565
20  0.95 1.038618
21  1.00 1.042915
22  1.05 1.047459
23  1.10 1.052255
24  1.15 1.057306
25  1.20 1.062619
26  1.25 1.068198
27  1.30 1.074049
28  1.35 1.080177
29  1.40 1.086589
30  1.45 1.093291
31  1.50 1.100290
32  1.55 1.107592
33  1.60 1.115206
34  1.65 1.123140
35  1.70 1.131402
36  1.75 1.140001
37  1.80 1.148946
38  1.85 1.158247
39  1.90 1.167915
40  1.95 1.177960
41  2.00 1.188395
42  2.05 1.199231
43  2.10 1.210482
44  2.15 1.222160
45  2.20 1.234281
46  2.25 1.246858
47  2.30 1.259910
48  2.35 1.273451
49  2.40 1.287500
50  2.45 1.302075
51  2.50 1.317197
52  2.55 1.332887
53  2.60 1.349166
54  2.65 1.366058
55  2.70 1.383589
56  2.75 1.401784
57  2.80 1.420671
58  2.85 1.440281
59  2.90 1.460644
60  2.95 1.481794
61  3.00 1.503767
62  3.05 1.526601
63  3.10 1.550335

Extract function exemplars into two vectors ..

In [7]:
[r|
   jay_theta <<- jaydata$Theta
   jay_A     <<- jaydata$A |]
0x00007fa4540c1840

So now let's do some EC.

Here's what ECcame up with ..

$\theta = \cfrac{2 \cdot (5A^8 + 2A^3 -A^2 -5)}{5A^7}$

Now let's put it into an R mapping function which takes A and yields $\theta$

In [23]:
[r|
  f_ec <<- function(A) {
     theta = (2*(5*A**8 + 2*A**3 - A**2 -5))/(5*A**7)
     return (theta)
     }|]
0x00007fa40c79a8f0

Make the predictions ..

In [24]:
[r|  ai_pred <<- unlist(lapply (jay_A,f_ec)) |]
0x00007fa4581099f0

And plot ...

In [25]:
[rgraph| 
    newFrame <<- data.frame (jay_theta, ai_pred)
    ggplotRegression("Actual vs Predicted for Theta", newFrame, jay_theta, ai_pred) |]
The MSE (mean square error) for the EC solution was,

${MSE} = 0.62$

I'll try to add some transindental functions ..

In [ ]: