How to use character variables in #formula macro? - julia

How can I use variables in #fomula()?
I'd like to write something like
var1 = "price + volume"
ModelFormula = #formula(dep_var ~ 0 + $var1)
Which would be interpreted as
#formula(dep_var ~ 0 + price + volume)

Read the documentation about programmatic model construction in StatsModels.jl (which you presumably use). In your case, it seems something like the following would be sufficient:
Term(:dep_var) ~ ConstantTerm(0) + Term(:price) + Term(:volume)
Or fold(+, Term.([:price, :volume])) if the names are given as an array.

...spent some time on this one. Please post answers if there are better ways.
Works this way:
var1 = "price + volume"
ModelFormula = #eval #formula(dep_var ~ 0 + $(Symbol(var1)))
Although, passing the whole expression "dep_var = 0 + price + volume" generates an error:
julia> formula = "a ~ 0 + b"
julia> #eval #formula($(Symbol(formula)))
ERROR: LoadError: type Symbol has no field head
edit2:
digging deeper, it seems that with this approach you have you pass each variable on it's own, so that #formula interprets each independently..
var1 = "price"
var2 = "volume"
ModelFormula = #eval #formula(dep_var ~ 0 + $(Symbol(var1)) + $(Symbol(var2)))

Related

Get-values from a html form in a for/do loop

I have a problem with get-value() method in progress4GL.
I am trying to get all values from html form.
My Progress4GL Code looks like:
for each tt:
do k = 1 to integer(h-timeframe):
h-from [k] = get-value(string(day(tt.date)) + "#" + string(tt.fnr) + "#" + string(tt.pnr) + "_von" + string(k)).
h-to [k] = get-value(string(day(tt.date)) + "#" + string(tt.fnr) + "#" + string(tt.pnr) + "_bis" + string(k)).
h-code [k] = get-value(string(day(tt.date)) + "#" + string(tt.fnr) + "#" + string(tt.pnr) + "_code" + string(k)).
end.
end.
h-timeframe is parameter and could be max. 10. (1-10)
tt is a temp-table and represents a week(fix 7 days)
It works perfectly till 9.Parameter. If I choose the 10 (which is max) then I get some performance Problem using get-value() Function.
Example when h-timeframe = 10:
as you can see from one get-value to another It takes really long time.( h-timeframe = 10 )
Example when h-timeframe = 9:
and here way much faster than other.
Can anyone explain why ? It is really strange and I have no Idea.
p.s: I have this problem just at 10. 0-9 It works perfectly
The performance difference is probably something external to your code snippet but, for performance, I would write it more like this:
define variable d as integer no-undo.
define variable n as integer no-undo.
define variable s as character no-undo.
for each tt:
// avoid recalculating and invoking functions N times per TT record
assign
d = day( tt.date )
n = integer( h-timeframe )
s = substitute( "&1#&2#&3_&&1&&2", d, tt.fnr, tt.pnr )
.
do k = 1 to n:
// consolidate multiple repeated operations, eliminate STRING() calls
assign
h-from [k] = get-value( substitute( s, "von", k ))
h-to [k] = get-value( substitute( s, "bis", k ))
h-code [k] = get-value( substitute( s, "code", k ))
.
end.
end.

Function not working as expected within code, but works perfect in console

I have R function [here is script, here is package] that works perfectly in console, but when I build and load package, something goes wrong
In console environment, I create vector, create function, assign the variable to pass to function, and output is as expected when I execute ListPalette(listname)
> PunjabiPalette <- list (
+ AmritsariKulcha = c("#e3e4d9", "#ebdc9c", "#b3340e", "#67140a", "#2a231d"),
+ CholeBhature = c("#7cab70", "#d9bf9c", "#a04d05", "#995f7e", "#972107"),
+ FieldsOfPunjab = c("#fda726", "#d75b07", "#702e06", "#514617", "#313407"),
+ FieldsOfPunjab2 = c("#9aa5b4", "#516e9c", "#13306a", "#94aa0b", "#a36316"),
+ GoldenTemple = c("#bdcad0", "#5f8abf", "#ffd860", "#d88821", "#672006"),
+ GoldenTemple2 = c("#7d84cb", "#374890","#c2592e", "#fa5102", "#722416"),
+ Pindh = c("#5eb39c", "#1f6562","#2168c2", "#d77e5f", "#5f3e25"),
+ SohniMahiwal = c("#dc6478", "#a9365a", "#f4420e", "#403c61", "#313f42"),
+ HeerRanjha = c("#93dd7d","#3272b6", "#ec9382", "#ab3a40", "#072246"),
+ Gidha = c("#fdea6e", "#4aec6a", "#fb7894", "#f13111", "#2584a0"),
+ Gidha2 = c("#fb9961", "#f13375", "#771341", "#2d3c2f", "#ea263c"),
+ Teej = c("#22325f", "#88ce64", "#fbd234", "#b8091f", "#682f4e"),
+ Phulkari = c("#efa20b", "#04a193", "#14555d","#820203", "#ed2e06"),
+ Phulkari2 = c("#9c1a41", "#42a4e8", "#3a35da", "#ee523c", "#3e167c"),
+ Jutti = c("#460809", "#00699e", "#391b72", "#03471d", "#ba0841"),
+ Jutti2 = c("#e278e5", "#13187e", "#fb6225", "#f23561", "#d2b88f"),
+ Jutti3 = c("#6fa42c", "#db3717", "#051a8d", "#ef38a7", "#202c3d"),
+ Paranda = c("#eaa32b", "#f45d59", "#c33dd2", "#92214c", "#201274")
+ )
> ListPalette <- function(listname){
+ names(listname)
+ }
> listname <- PunjabiPalette
> ListPalette(listname)
[1] "AmritsariKulcha" "CholeBhature" "FieldsOfPunjab" "FieldsOfPunjab2" "GoldenTemple" "GoldenTemple2"
[7] "Pindh" "SohniMahiwal" "HeerRanjha" "Gidha" "Gidha2" "Teej"
[13] "Phulkari" "Phulkari2" "Jutti" "Jutti2" "Jutti3" "Paranda"
>
However, when I run the same script, build the package locally and execute ListPalette(listname), I get following
> ListPalette("RanglaPunjab")
NULL
Something is amiss. I thought it might be a silly oversight, but I've been wrangling over this for more than an hour .... please guide.
Try it without quotes.
ListPalette(RanglaPunjab)
If you want to use the name of the list as a character, you must use get.
ListPalette <- function(listname){
list <- get(listname)
names(list)
}
ListPalette("PunjabiPalette")
[1] "AmritsariKulcha" "CholeBhature" "FieldsOfPunjab" "FieldsOfPunjab2" "GoldenTemple" "GoldenTemple2" "Pindh"
[8] "SohniMahiwal" "HeerRanjha" "Gidha" "Gidha2" "Teej" "Phulkari" "Phulkari2"
[15] "Jutti" "Jutti2" "Jutti3" "Paranda"

How to detect numbers from one column in another and write data from another column in R?

I'm very new at R language. I tried to make simple code to find some data in different intervals with information about start-end of interval and table with bigger intervals for now, I got several codes for solving but none of them work.
The main idea is that I have several variables which represent different arrays (i,k,j). Code, in my opinion, should look for each array in another table for two things (if it bigger than the first column and if it smaller then second, if both true - right all this to another table and go to other intervals).
if(mydatatable[k,21]>=mydatatable[i,16]){
if(mydatatable[k,21]<=mydatatable[j,18])
shifr[n,1]<-n&shifr[n,2]<-mydata[k,21]&shifr[n,3]<-mydata[k,22]&i+1&j+1&k+1&n+1
else i+1&j+1&k+1
}
else {
if(mydatatable[i,16]==0) end
else i+1&j+1&k+1
}
for this code several errors
Error in if (mydatatable[k, 21] >= mydatatable[i, 16]) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In Ops.factor(mydatatable[k, 21], mydatatable[i, 16]) :
‘>=’ not meaningful for factors
I wonder, why programm thinks, that mydatatable is factor? Why it should be some TRUE/FALSE value, I thought, that it was already established in formula.
The second code is pretty much the same and it even might work.
I preestablished the values i,k and j as 1 (i=1, k=1, j=1)
But there comes a error
> ifelse(mydatatable[k,21]>=mydatatable[i,16],
+ ifelse(mydatatable[k,21]<=mydatatable[j,18],
+ shifr[n,1]<-n&shifr[n,2]<-mydata[k,21]&shifr[n,3]<-mydata[k,22]&i+1&j+1&k+1&n+1,i+1&j+1&k+1),
+ ifelse(mydatatable[i,16]==0,
+ end),
+ i+1&j+1&k+1)
Error in ifelse(mydatatable[k, 21] >= mydatatable[i, 16], ifelse(mydatatable[k, :
unused argument (i + 1 & j + 1 & k + 1)
I'm confused, why it's happening.
Please, help.
Here is an example of data. What I want to get is demonstrated here https://1drv.ms/f/s!Aj4W4aYSeYkGiIFKHG0TV-TRQvWaIQ
Here what I got, after several use of data.table::foverlaps (I fixed some problems, but initially I got this)
> data.table::foverlaps(int1,sh2,by.x=c("start","end"),by.y=c("start","end"))
Error in data.table::foverlaps(int1, sh2, by.x = c("start", "end"), by.y = c("start", :
The first 2 columns of y's key is not identical to the columns specified in by.y.
I have also got some progress with previous code. Several problems: first, how to unite several commands (I used & and ; but none of them worked properly in shifr[n,1]<-n;shifr[n,2]<-sh[k,1];shifr[n,3]<-sh[k,1];i+1&j+?)
Second, is about Error in FUN(left, right).
> ifelse(sh[k,1]>=int[i,1],
+ + ifelse(sh[k,2]<=int[j,2],
+ + shifr[n,1]<-n;shifr[n,2]<-sh[k,1];shifr[n,3]<-sh[k,1];i+1;j+1;k+1;n+1,i+1;j+1;k+1),
Error: unexpected ';' in:
" + ifelse(sh[k,2]<=int[j,2],
+ shifr[n,1]<-n;"
> + i+1;j+1;k+1
[1] 16
[1] 16
[1] 65
> ifelse(sh[k,1]>=int[i,1],
+ + ifelse(sh[k,2]<=int[j,2],
+ + shifr[n,1]<-n;shifr[n,2]<-sh[k,1];shifr[n,3]<-sh[k,1];i+1&j+1&k+1&n+1,i+1&j+1&k+1),
Error: unexpected ';' in:
" + ifelse(sh[k,2]<=int[j,2],
+ shifr[n,1]<-n;"
> + i+1&j+1&k+1
[1] TRUE
> ifelse(sh[k,1]>=int[i,1],
+ + ifelse(sh[k,2]<=int[j,2],
+ + shifr[n,1]<-n&shifr[n,2]<-sh[k,1]&shifr[n,3]<-sh[k,1]&i+1&j+1&k+1&n+1,i+1&j+1&k+1),
+ + i+1&j+1&k+1
+ )
Error in FUN(left, right) :
operations are possible only for numeric, logical or complex types
This code is, actually, awful. I eventually done it like that(in python):
for i in interval:
...
if float(i) < intervals[b][0]:
print("Interval are too high", intervals[b][1])
break
....
if float(i) >= intervals[b - 1][1]:
....

Using solve and/or linsolve with the symbolic toolbox in R2010b

I asked a question a few days ago here and got an answer that seems like it would work- it involves using linsolve to find the solutions to a system of equations that are all modulo p, where p is a non-prime integer.
However, when I try to run the commands from the provided answer, or the linsolve help page, I get an error saying linsolve doesn't support arguments of type 'sym'. Is using linsolve with sym variables only possible in R2013b? I've also tried it with my school's copy, which is R2012b. Here is the code I'm attempting to execute (from the answer at the above link):
A = [0 5 4 1;1 7 0 2;8 1 0 2;10 5 1 0];
b = [2946321;5851213;2563617;10670279];
s = mod(linsolve(sym(A),sym(b)),8)
And the output is:
??? Undefined function or method linsolve' for input arguments of type 'sym'.
I've also tried to use the function solve for this, however even if I construct the equations represented by the matrices A and b above, I'm having issues. Here's what I'm attempting:
syms x y z q;
solve(5*y + 4*z + q == 2946321, x + 7*y + 2*q == 5851213, 8*x + y + 2*q == 2563617, 10*x + 5*y + z == 10670279,x,y,z,q)
And the output is:
??? Error using ==> char
Conversion to char from logical is not possible.
Error in ==> solve>getEqns at 169
vc = char(v);
Error in ==> solve at 67
[eqns,vars] = getEqns(varargin{:});
Am I using solve wrong? Should I just try to execute my code in R2013b to use linsolve with symbolic data types?
The Symbolic Math toolbox math toolbox has changed a lot (for the better) over the years. You might not have sym/linsolve, but does this work?:
s = mod(sym(A)\sym(b),8)
That will basically do the same thing. sym/linsolve just does some extra input checking and and rank calculation to mirror the capabilities of linsolve.
You're using solve correctly for current versions, but it looks like R2010b may not understand the == operator (sym/eq) in this context. You can use the old string format to specify your equations:
eqs = {'5*y + 4*z + q = 2946321',...
'x + 7*y + 2*q = 5851213',...
'8*x + y + 2*q = 2563617',...
'10*x + 5*y + z = 10670279'};
vars = {'x','y','z','q'};
[x,y,z,q] = solve(eqs{:},vars{:})

trapezodial rule matlab

I want to integrate "\int_{0}^{1}(exp(-int_{0}^{y}f(x)dx))dy" with my basic trapezoid algorithm. I recieve an error declaration, but I should define g as a function. Do you have any idea how to do it?
Thanks a lot for any answer!
function y = trapapadbl(low1, up1,low2,up2,intstep1,intstep2,f)
g = 0;
step1 = (up1 - low1) / intstep1;
step2 = (up2 - low2) / intstep2;
for j = low1 : step1 : up1
g = g + feval(f,j);
end
g = #(y)(g - (feval(f, low1) + feval(f, up1))/2) * step1;
for i = low2 : step2 : up2
y= y + feval(g,i);
end
y= (y - (feval(g, low2) + feval(g, up2))/2) * step2;
>> trapapadbl(0,1,0.1,0,1,0.1,#sin)
??? Undefined function or variable "y".
Error in ==> trapapadbl at 12
y= y + feval(g,i);
Without working too hard to try to understand your code (!) the error is that y was never initialized. You can't add anything to y until it has a value. When I initialize y to 0, the code runs, but I get 0 as an output, which is not what happens when you integrate sin from 0 to 1. I may be calling the function wrong, but it's something to look out for!
Furthermore, your code is confusing, because you use the variable g as both a double (a number) and a function, even in the same line! The same problem happens as y is the input to your anonymous function, but also a double later on. It's syntactically correct, but a little hard to read. Consider using a different variable name, or including clear comments (or both!)

Resources