Can this query be shorter? Sqlite - sqlite

Keep in mind that I use sqlite so I can't use stuff like:
(SELECT Z1, Z2, Z3, DECODE(Z1, 'x', 1, 0) + DECODE(Z2, 'x', 1, 0) + DECODE(Z3, 'x', 1, 0) test FROM InfoP1)
This is my query:
String sql = "SELECT ID, Nume, Prenume,"
+ "InfoP1.Z1, InfoP1.Z2, InfoP1.Z3, "
+ "SUM(InfoP1.Z1 + InfoP1.Z2 + InfoP1.Z3) AS Total, "
+ "(SELECT(((CASE WHEN Z1 = 'x' then 1 else 0 END)"
+ "+ (CASE WHEN Z2 = 'x' then 1 else 0 END)"
+ "+ (CASE WHEN Z3 = 'x' then 1 else 0 END))*8) AS Test),"
+ "FROM InfoAn "
+ "LEFT JOIN InfoP1 ON InfoAn.ID = InfoP1.rowid "
+ "GROUP BY ID";

In SQLite, boolean expressions return 0 or 1, so you can simply drop the decoding:
SELECT Z1, Z2, Z3, (Z1 = 'x') + (Z2 = 'x') + (Z3 = 'x') AS test FROM InfoP1

Related

Error: Aesthetics must be either length 1 or the same as the data (121): yintercept

I have the following data (from 1 to 1032) and I am trying to plot correlogram for autocorrelation and partial autocorrelation:
prp: data frame
prp$Log.prp.Standardized: the column that I am trying to plot
Data:
prp$Log.prp.Standardized (Name of the column - I have 1 column with 1032 values)
1 1.7923928339
2 0.7792383013
3 -0.2033400303
4 -1.7016479357
5 0.8002357419
6 0.3575677621
7 1.0209246410
8 0.7188631605
9 -0.5320108464
10 -0.2190886401
.
.
.
.
(till 1032)
The function that I am using:
correlogram <- function(x, type = "correlation"){
gacf = acf(x, plot=FALSE, lag.max=120, type = type)
gacf.df = with(gacf, data.frame(lag, acf))
gacf.df$sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
q <- ggplot(data = gacf.df, mapping = aes(x = lag, y = acf))
q <- q + xlim(c(0,120)) + theme_bw()
q <- q + geom_hline(aes(yintercept = 0))
q <- q + geom_segment(mapping = aes(xend = lag), yend = 0, lwd = 1)
q <- q + geom_hline(aes(yintercept = c(sig, -1*sig)), linetype = 2, colour = "#e51843")
if(type == "partial"){
q <- q + ylab(expression(alpha[k]))
} else {
q <- q + ylab(expression(rho[k]))
}
q <- q + xlab("lag k")
}
Then the code I am running:
require(gridExtra)
library(gridExtra)
library(ggplot2)
library(grid)
q1 <- correlogram(prp$Log.prp.Standardized) + xlab(" ") + ggtitle("Total and Partial Correlograms")
q2 <- correlogram(prp$Log.prp.Standardized, type = "partial")
grid.arrange (q1, q2, nrow = 2)
grid
But I am getting the following error:
Error: Aesthetics must be either length 1 or the same as the data (121): yintercept
Any help will be appreciated!
The issue is that you map c(sig, -1*sig) on yintercept which will not work as the length of c(sig, -1*sig) is two times the length of your df gacf.df. That's what the error message is telling you. There are two options to achieve your desired result:
If you add sig as a variable you have to add the horizontal lines via two calls of geom_hline.
The approach below instead makes sig a scalar. In that case you don't have to wrap yintercept = c(sig, -1*sig) inside aes() :
correlogram <- function(x, type = "correlation"){
gacf = acf(x, plot=FALSE, lag.max=120, type = type)
gacf.df = with(gacf, data.frame(lag, acf))
#gacf.df$sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
sig = qnorm((1 + 0.95)/2)/sqrt(length(x))
q <- ggplot(data = gacf.df, mapping = aes(x = lag, y = acf))
q <- q + xlim(c(0,120)) + theme_bw()
q <- q + geom_hline(aes(yintercept = 0))
q <- q + geom_segment(mapping = aes(xend = lag), yend = 0, lwd = 1)
# q <- q + geom_hline(aes(yintercept = sig), linetype = 2, colour = "#e51843")
# q <- q + geom_hline(aes(yintercept = -1*sig), linetype = 2, colour = "#e51843")
q <- q + geom_hline(yintercept = c(sig, -1*sig), linetype = 2, colour = "#e51843")
if(type == "partial"){
q <- q + ylab(expression(alpha[k]))
} else {
q <- q + ylab(expression(rho[k]))
}
q <- q + xlab("lag k")
}
library(gridExtra)
library(ggplot2)
library(grid)
set.seed(42)
prp <- data.frame(Log.prp.Standardized = rnorm(100))
q1 <- correlogram(prp$Log.prp.Standardized) + xlab(" ") + ggtitle("Total and Partial Correlograms")
q2 <- correlogram(prp$Log.prp.Standardized, type = "partial")
grid.arrange (q1, q2, nrow = 2)
Created on 2021-02-18 by the reprex package (v1.0.0)

how to insert more densely sampled auxiliary variables into the georob package in Rstudio

I need to make a prediction of a soil variable as a function of auxiliary variables in the georob package.
My solo dataset has 200 observations and my auxiliary variables set has 19940 data, however in the code, I can't enter the coordinates of the auxiliary variables as prediction points.
dat= read.csv("malhas amostrais/solo_200.csv", sep = ",")
covar = read.csv("../dados/csv/variaveis_auxiliares.csv", sep = ";")
ku_georob_cpeso <- georob(argila ~ CV + CH + dist_bebedouros + Eca_0.5m + Eca_1m + elevacao + IH_0.5m + sd_ndvi_01 + sd_ndvi_02 + twi + S_P_T + sd_b4 +sd_b5 + sd_b6+ sd_b7,
data= dat,
locations= ~ x + y,
variogram.model="RMexp",
param=c(variance=200, nugget=600, scale=150),
verbose = 3,
psi.func = "huber")
ku_georob_cpeso <- georob(argila ~ CV + CH + dist_bebedouros + Eca_0.5m + Eca_1m + elevacao + IH_0.5m + sd_ndvi_01 + sd_ndvi_02 + twi + S_P_T + sd_b4 +sd_b5 + sd_b6+ sd_b7,
data= dat1,
subset = cova,
locations= ~ x + y,
variogram.model="RMexp",
param=c(variance=200, nugget=600, scale=150),+ verbose = 3,
psi.func = "huber")
I receive the error:
Error in xj[i] : invalid subscript type 'list'

Removing the interaction terms when the main effect is removed

I have a formula in R for example
y ~ x + z + xx + zz + tt + x:xx + x:zz + xx:z + zz:xx + xx:zz:tt
or even more complicated (y~x*z*xx*zz*tt)
Note that the names on the right-hand side of the formula are intentionally selected to be somehow similar to at least one other term.
The question is now how to remove the interaction terms that are related to a specific main effect. For example, if I remove the term x (main effect) I want to remove the interaction terms that also include x, here x:xx.
I have tried grepl() but it would remove any term that contains partially or fully the word. In my example it removes x,xx,x:xx,xx:z,zz:xx,xx:zz:tt
any ideas about a function to do it?
Update:
What I have already tried:
f = y ~ x + z + xx + zz + tt + x:xx + x:zz + xx:z + zz:xx + xx:zz:tt
modelTerms = attr(terms(f) , which = 'term.labels')
modelTerms[!grepl(pattern = 'x', x = modelTerms)]
Use update.formula:
f <- y~x*z*xx*zz*tt
update(f, . ~ . - x - x:.)
#y ~ z + xx + zz + tt + z:xx + z:zz + xx:zz + z:tt + xx:tt + zz:tt +
# z:xx:zz + z:xx:tt + z:zz:tt + xx:zz:tt + z:xx:zz:tt
f <- y ~ x + z + xx + zz + tt + x:xx + x:zz + xx:z + zz:xx + xx:zz:tt
update(f, . ~ . - x - x:.)
#y ~ z + xx + zz + tt + z:xx + xx:zz + xx:zz:tt
Are you looking for this?
> modelTerms[!grepl(pattern='^x\\:x+', x=modelTerms)]
[1] "x" "z" "xx" "zz" "tt" "x:zz" "z:xx" "xx:zz"
[9] "xx:zz:tt"
Simple:
f = y~x*z*xx*zz*tt
modelTerms = attr(terms(f) , which = 'term.labels')
l = sapply(
strsplit(x = modelTerms, split = '[:*]'),
FUN = function(x) {
'x' %in% x
}
)
modelTerms[!l]

Equal spacing with multiple atop

I'm trying to create a legend in a ggplot2 graph with multiple lines and a parameter and value on each line. Since I have symbols as variables, this needs to be done with expression. To create new lines, I have used multiple atop commands, but this leads to uneven spacing in the final line. Please see my following example:
library(ggplot2)
N = 25
a = -5
b = 2
sigma = 1
x = runif(N, 0, 10)
y = a + x * b + rnorm(N, sd = sigma)
df = data.frame(x, y)
ggplot(df, aes(x, y)) +
geom_point() +
geom_label(aes(x = 1, y = max(y) - 2),
label = paste0("atop(atop(",
"textstyle(a == ", a, "),",
"textstyle(b == ", b, ")),",
"textstyle(sigma == ", sigma, "))"
), parse = TRUE
)
ggsave("plotmath_atop.png", width = 6, height = 4, scale = 1)
This gives the following plot:
As you can see, the spacing between the lines b=2 and \sigma=1 is noticeably larger than the spacing between the lines a=-5 and b=2.
Is there a way of using expression with multiple line breaks while still having even spacing between each line?
you could use gridExtra::tableGrob,
library(gridExtra)
library(grid)
table_label <- function(label, params=list()) {
params <- modifyList(list(hjust=0, x=0), params)
mytheme <- ttheme_minimal(padding=unit(c(1, 1), "mm"),
core = list(fg_params = params), parse=TRUE)
disect <- strsplit(label, "\\n")[[1]]
m <- as.matrix(disect)
tg <- tableGrob(m, theme=mytheme)
bg <- roundrectGrob(width = sum(tg$widths) + unit(3, "mm"), height = sum(tg$heights) + unit(3, "mm"))
grobTree(bg, tg)
}
txt <- 'a == -5\n
b == 2\n
sigma == 1'
library(ggplot2)
qplot(1:10,1:10) +
annotation_custom(table_label(txt), xmin=0, xmax=5, ymin=7.5)
A simple solution is to avoid the use of expressions, print the sigma letter using the unicode character \u03c3, and use \n for line breaking.
library(ggplot2)
N = 25
a = -5
b = 2
sigma = 1
df = data.frame(runif(N, 0, 10), a + x * b + rnorm(N, sd = sigma))
lab <- paste0("a = ", a, "\n",
"b = ", b, "\n",
"\u03c3 = ", sigma)
ggplot(df, aes(x, y)) +
geom_point() +
geom_label(aes(x = 1, y = max(y) - 2), label = lab, parse = FALSE)
ggsave("plot_multiline_label.png", width = 6, height = 4, scale = 1)

What value is returned by the call f(4,2)?

//pseudocode
//n and k are nonnegative integers`
int f(int n, int k)`
`if(k*n ==0)`
`return 1`
`else`
`return f(n-1,k-1)+f(n-1,k)`
`end if`
end f
What I have so far is this. But how do I find the value of this function call?
f(3,1) + f(3,2)
f(2,0) +f(2,1) f(2,1)+f(2,2)
1 f(1,0)+f(1,1) f(1,1)+f(1,2)
f(0,0)+f(0,1) f(0,1)+f(0,2)
Basically the answer is just continuing what you started:
f(4, 2) ; ==
f(3, 1) + f(3, 2) ; ==
f(2, 0) + f(2, 1) + f(2, 1) + f(2, 2) ; ==
1 + f(1, 0) + f(1, 1) + f(1, 0) + f(1, 1) + f(1, 1) + f(1, 2) ; ==
1 + 1 + f(0, 0) + f(0, 1) + 1 + f(0, 0) + f(0, 1)+ f(0, 0) +
f(0, 1) + f(0, 1) + f(0, 2) ; ==
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ; ==
11
The answer is 11.
One simple way to understand recursion function call is to expand the recursion call stack tree, which can help you clearly see how the whole process goes(Hope the pic is clear enough):
In the comment I mentioned divede-and-conquer process in a recursion process, so I updated a new image to contain the whole process. Hope this could be helpful :-)

Resources