简体   繁体   中英

nested data with map2 and rstandard

i have nested data:

data_nested<-data_model2%>% group_by(NAPLAN_YEAR, Year_Level, DOMAIN_NAME)%>% nest()

and i need to run a linear regression and get standardised residuals

data_nested<-data_nested%>%
  mutate(

    model1=map(data, ~ lm(
      SCALE_SCORE_0~TWO_YEAR_PRIOR_SCALE_SCORE_0+PV1.Dim1, 
      data=.x, na.action=na.exclude)),

    # resids 
    stand_resids1 = map2(data, model1, ~rstandard(data=.x))
)

and then i will be using only stand_resids variable so will need to unnest it....

I tried using map with rstandard and it "sort of" worked but when unnesting, R freezes and does not let me do anything...

With map2 it gives me this error

Caused by error in `UseMethod()`:
! no applicable method for 'rstandard' applied to an object of class "c('tbl_df', 'tbl', 'data.frame')" 

I am lost.. what am i doing wrong?

how can i get my standardised residuals and unnest safely?

My data

structure(list(NAPLAN_YEAR = c(2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L), Year_Level = c("YR5", 
"YR5", "YR9", "YR9", "YR9", "YR9", "YR9", "YR9", "YR9", "YR9", 
"YR9", "YR9", "YR9", "YR9", "YR9", "YR9", "YR9", "YR9", "YR9", 
"YR9"), DOMAIN_NAME = c("READING", "NUMERACY", "NUMERACY", "READING", 
"NUMERACY", "READING", "READING", "NUMERACY", "READING", "NUMERACY", 
"READING", "NUMERACY", "READING", "NUMERACY", "READING", "NUMERACY", 
"READING", "NUMERACY", "READING", "NUMERACY"), SCALE_SCORE_0 = c(483.5, 
361.4, 472.3, 472.7, 618.1, 597.9, 493.7, 579.8, 607, 598.7, 
537.5, 560.8, 655.1, 637.5, 589, 631.6, 680.3, 638.6, 554.5, 
535), TWO_YEAR_PRIOR_SCALE_SCORE_0 = c(551.2, 494.3, 393.7, 433.8, 
572, 555.2, 579.7, 533.4, 547, 607, 555.2, 507.5, 650.5, 682.1, 
538.8, 649.1, 631.5, 607, 505.4, 533.4), PV1.Dim1 = c(0.519712082711055, 
0.519712082711055, 0.816107220452504, 0.816107220452504, 0.694822268857737, 
0.694822268857737, 2.54441143140355, 2.54441143140355, 0.70926716635939, 
0.70926716635939, -1.09457147222467, -1.09457147222467, 1.28512986405404, 
1.28512986405404, 0.999450010152358, 0.999450010152358, 0.275612418980028, 
0.275612418980028, 1.70528701197301, 0.993431776355567)), row.names = c(NA, 
-20L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x7fbafa00d0e0>)

According to ?rstandard , data is not one of the arguments to the function

rstandard(model, ...)

S3 method for class 'lm'

rstandard(model, infl = lm.influence(model, do.coef = FALSE), sd = sqrt(deviance(model)/df.residual(model)), type = c("sd.1", "predictive"), ...)


Thus, if we apply the function on the model it should work

library(dplyr)
library(purrr)
data_nested2 <- data_nested%>% ungroup %>%
  mutate(

    model1=map(data, ~ lm(
      SCALE_SCORE_0~TWO_YEAR_PRIOR_SCALE_SCORE_0+PV1.Dim1, 
      data=.x, na.action=na.exclude)), stand_resid1 = map(model1, rstandard)) 

-output

> data_nested2$stand_resid1
[[1]]
  1 
NaN 

[[2]]
  1 
NaN 

[[3]]
         1          2          3          4          5          6          7          8          9 
-0.6282880  1.2421548  1.3180234 -0.5979447  0.1707562 -0.9341692 -0.2240499  1.1827930 -1.4042771 

[[4]]
         1          2          3          4          5          6          7          8          9 
-0.2050853  0.4003123 -2.1822759  0.7372813 -2.0895189  0.1957979  0.5825138  0.8302289  0.6904705 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM