R lmer and SAS PROC GLIMMIX for multilevel linear modeling and logistic modeling

The following R and SAS syntaxes returned almost identical results.  (I was a bit surprised that they used almost identical degree of freedom - where group variables uses the n of groups minus something.)

LINEAR MODELING

R:

library(lme4)

kaz1 <- lmer(formula=SAT_TOTAL ~ treat + pretest_GPA + male + minority + disadv + (1|group_school),
data= sas_data_Sat, family = gaussian, REML = TRUE, verbose = FALSE)
summary(kaz1)

SAS:

proc glimmix data=sample ;
where flag_SAT=1 ;
class group_school ;
model SAT_TOTAL=treat pretest_GPA male minority disadv/
solution ddfm=kr dist=normal link=identity;
random intercept /subject=group_school;
run;

 

NON-LINEAR MODELING (I don't like the degree of freedom option, though. KR is not working. See the syntax at the bottom of this page for specifying DF)

SAS:

proc glimmix data=sample METHOD=LAPLACE ;
*where flag_CE=1 ;
where flag_SAT=1 ;
*where flag_DE=1 ;
class group_school ;
model enroll_fall=treat pretest_GPA male minority disadv /
solution /*ddfm=kr*/ dist=binomial link=logit ;
random intercept /subject=group_school;
run;

R:

m <- glmer(enroll_fall ~ treat + pretest_GPA + male + minority + disadv +
(1 | group_school), data = sas_data_Sat, family = binomial, control = glmerControl(optimizer = "bobyqa"),
nAGQ = 10)
#print(m, corr = FALSE)
summary(m)

 

One can specify DF this way.

proc glimmix data=whole METHOD=LAPLACE ;
by subgroup;
where flag_SAT=1 ;
class group_school ;
model &var1=treat C_pretest_GPA male minority disadv /
solution /*ddfm=kr*/ dist=binomial link=logit
ddf=36.157,.,.,.;
random intercept /subject=group_school;
ods output ParameterEstimates=_&var1;
run;

 

Need help with R

Thanks for helping me with R's ifelse.

This creates a fake dataset.
test = expand.grid(a = c(1, 2, 3, 4), b = c(1, 2, 3))

What I want to do is to something like this:

if a=3 then do;

if b=1 then news="3 and 1";

if b=2 then news="3 and 2";

end;

 

This below does half of the job.  Could you advise?

test$news <- ifelse(test$a == 3,ifelse(test$b == 1, "3 and 1",""),"")

Please email me at k u e k a w a @gmail.com .

Thanks.

R function example

library(broom)
library(compute.es)
library(dplyr)
library(forcats)
library(FSA)
library(gapminder)
library(ggplot2)
library(gmodels)
library(haven)
library(here)
library(leaflet)
library(magrittr)
library(markdown)
library(MatchIt)
library(plyr)
library(psych)
library(purrr)
library(readr)
library(readxl)
library(sas7bdat)
library(sf)
library(sqldf)
library(stringr)
library(summarytools)
library(tidyverse)
library(tmap)
library(tmaptools)
library(descr)
library(writexl)

library(tidyverse)

table1

show_example<-function(var1,var2){

var1_d <-deparse(substitute(var1))
var2_d <-deparse(substitute(var2))

table1 %>%
mutate(prop_cases=table1[[var1_d]]/table1[[var2_d]]) -> temp123
Summarize(temp123$prop_cases ~ country, data= temp123) -> kaz

}

result1<-show_example(var1=cases,var2=population)
result2<-show_example(var1=cases,var2=cases)

R Function example -- from Datacamp.com

https://campus.datacamp.com/courses/introduction-to-writing-functions-in-r/all-about-arguments?ex=2

 

# Set the default for n to 5
cut_by_quantile <- function(x, n, na.rm, labels, interval_type) {
probs <- seq(0, 1, length.out = n + 1)
qtiles <- quantile(x, probs, na.rm = na.rm, names = FALSE)
right <- switch(interval_type, "(lo, hi]" = TRUE, "[lo, hi)" = FALSE)
cut(x, qtiles, labels = labels, right = right, include.lowest = TRUE)
}

# Remove the n argument from the call
cut_by_quantile(
n_visits,
n = 5,
na.rm = FALSE,
labels = c("very low", "low", "medium", "high", "very high"),
interval_type = "(lo, hi]"
)

R

When two variables are similar but different by training empty spaces, do this.

levels(as.factor(main$county))

levels(as.factor(arc$county))

I tried to merge two data tables that contain the same variable county.   The simple merge did not work.  It turns out that one variable had trailing empty spaces.

R string macro

https://notstatschat.rbind.io/2018/07/30/quoting-and-macros-in-r/

Also

#id dups
#create fake data
junk <- c(1, "J", 1, "J", 1, "K", 2, "K", 3, "K", 3, "T", 3, "T", 3, "T")
junk_mat <- data.frame(matrix(junk,ncol=2, byrow=T))
#install.packages("data.table") # install it
library(data.table)
junk_tab <- data.table(junk_mat)
colnames(junk_tab) <- c("num_var", "char_var")
junk_tab$num_var <- as.numeric(junk_tab$num_var)
junk_tab

library(psych)

#this one works with char_var in quotes
kaz_macro1<-function(var1,var2){
var1<-describeBy(x=as.numeric(junk_tab$num_var), group=junk_tab[[var2]])
print(var1)
}
kaz_macro1(var1=kc1a,var2="char_var")

#this one works without char_var in quotes
kaz_macro1<-function(var1,var2){
temp_var <-deparse(substitute(var2))
var1<-describeBy(x=as.numeric(junk_tab$num_var), group=junk_tab[[temp_var]])
print(var1)
}
kaz_macro1(var1=kc1a,var2=char_var)