# Kenward-Roger Degrees of Freedom (SAS GLIMMIX)

I will edit this essay later.

Imagine I have an HLM model where level-1 are students and level-2 are schools.  I can enter teacher-level variables, but people who are used to HLM software by SSI will wonder how the use of teacher-level variables is possible without making the model 3-level models (level1 students, level2 teachers, level3 schools).  This is because in the old HLM tradition (people who studied HLM using HLM software in 1990s), equations are written/processed separately by levels and data must be prepared by levels (student data file and school data file).

If there are student-level and school-level, some HLM users may wonder how teacher-level variables can enter the model (or prepared in what file? student-level file or school-level file???).

People who use SAS or other software programs (maybe R?) will not wonder this because they think in terms of one whole equation and say, "of course we can enter variables of any levels."

The software programs they use adjust degree of freedom to take into consideration that teacher-level variables have a lot less number of values compared to the outcome variable.

K-R option in PROC GLIMMIX adjusts degree of freedom to account for the fact that group-level variables have a lot less possible values when compared to outcome variables.

K-R degree of freedom option seems most appropriate for multilevel modeling applied in educational evaluation studies (where typically students are nested within schools).  This option kicks in only when at least one random coefficient is estimated (e.g., intercepts as random effects).

proc glimmix data=i3d;
class school ;
model y =< here variable names >
/solution ddfm=kr dist=normal link=identity s ;
random int /sub=school;
run;

After playing with data type of simulation (I will do a better and well-documented simulation in the future), I learned the following:

For student-level (level-1) variables, degree of freedom under KR option is close to the number of students (minus a small number of cases maybe close to the number of predictors).  DF is larger if variance contained in the variable is greater.  For binary variables, DF is the largest when variance is .50 (DF gets smaller as a proportion gets close to 0 or 1).

For school-level (level2) variables, DF is close to the number of schools minus a small number maybe close to a number of predictors.  This may be also adjusted by variance of the variable.

I created a fake teacher-level predictors by creating two possible numeric values per school.  DF for this variable was close to the number of teachers in the data (two per school) minus a small number.  I think this is also adjusted by variance of the variable (which I didn't test exactly).
{"ft_lang_label":"__label__en","ft_lang_prob":0.9191897,"math_prob":0.81111026,"size":2713,"snap":"2021-43-2021-49","text_gpt3_token_len":569,"char_repetition_ratio":0.14950167,"word_repetition_ratio":0.027149322,"special_character_ratio":0.20862514,"punctuation_ratio":0.08414873,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96718484,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-23T10:48:36Z\",\"WARC-Record-ID\":\"<urn:uuid:4a0f215a-b490-4fd5-a78b-92adc6df812e>\",\"Content-Length\":\"78887\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fed26804-7db0-4143-bfe0-4004f06ed189>\",\"WARC-Concurrent-To\":\"<urn:uuid:d6acbfa4-2076-4501-bd45-a0e30c1d63fb>\",\"WARC-IP-Address\":\"173.236.241.31\",\"WARC-Target-URI\":\"https://www.estat.us/kenward-roger-degrees-of-freedom-sas-glimmix/\",\"WARC-Payload-Digest\":\"sha1:YTMPJPJLD2JW5DS272GV7M5WQ4JNTNXU\",\"WARC-Block-Digest\":\"sha1:43JKHU65LWUJHGLW62N67PZ7WZCJ4PWO\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585671.36_warc_CC-MAIN-20211023095849-20211023125849-00035.warc.gz\"}"}