setwd('/Users/shuseiyokoi/Desktop/shutats/HLM')
df = read.csv("pokemon.csv")
ggplot(data = df, aes(y= Attack , x = Defense, color =Type.1) ) +
geom_point()

modelb = lm(Attack ~ Defense, data = df)
summary(modelb)
##
## Call:
## lm(formula = Attack ~ Defense, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -140.304 -19.203 -4.039 16.234 125.584
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 45.28420 2.65383 17.06 <2e-16 ***
## Defense 0.45661 0.03311 13.79 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 29.19 on 798 degrees of freedom
## Multiple R-squared: 0.1924, Adjusted R-squared: 0.1914
## F-statistic: 190.2 on 1 and 798 DF, p-value: < 2.2e-16
ggplot(data = df, aes(y= Attack , x = Defense,color =Type.1) ) +
geom_point(aes(color =Type.1))+
geom_smooth(method=lm, se=FALSE, fullrange=T)+
geom_abline(intercept = 45.28420, slope = 0.45661, size = 1)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ x'

ggplot(df) +
aes(x = Defense, y = Attack) +
stat_smooth(method = "lm", se = FALSE) +
geom_point() +
geom_abline(intercept = 45.28420, slope = 0.45661, size = 1) + #fixed part of model1
facet_wrap(~Type.1) +
theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

merTools::ICC(outcome = 'Attack', group = 'Defense', data = df)
## [1] 0.2788982
model1 = lmer(Attack ~ Defense + (1 + Defense | Type.1), data = df)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, :
## Model failed to converge with max|grad| = 1.42345 (tol = 0.002, component 1)
## Warning in checkConv(attr(opt, "derivs"), opt$par, ctrl = control$checkConv, : Model is nearly unidentifiable: very large eigenvalue
## - Rescale variables?
summary(model1, corr=F)
## Linear mixed model fit by REML ['lmerMod']
## Formula: Attack ~ Defense + (1 + Defense | Type.1)
## Data: df
##
## REML criterion at convergence: 7608.3
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -4.6706 -0.6425 -0.1311 0.5187 4.8451
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## Type.1 (Intercept) 463.23365 21.5229
## Defense 0.07763 0.2786 -0.90
## Residual 738.57885 27.1768
## Number of obs: 800, groups: Type.1, 18
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 44.68062 6.04617 7.390
## Defense 0.49710 0.07811 6.364
## optimizer (nloptwrap) convergence code: 0 (OK)
## Model failed to converge with max|grad| = 1.42345 (tol = 0.002, component 1)
## Model is nearly unidentifiable: very large eigenvalue
## - Rescale variables?
fixef(model1)
## (Intercept) Defense
## 44.6806214 0.4970961
ranef(model1)
## $Type.1
## (Intercept) Defense
## Bug -1.65489024 -0.088804864
## Dark 20.63474624 -0.195328122
## Dragon -10.22342946 0.358546055
## Electric 1.13561226 -0.124824074
## Fairy -20.18076262 0.142343389
## Fighting 1.77083104 0.195486653
## Fire 8.38802594 -0.042083231
## Flying -6.77824607 0.104634161
## Ghost 4.35268887 -0.168964980
## Grass -9.50381544 0.050151994
## Ground 0.06393304 0.085851005
## Ice -4.88047726 -0.009239055
## Normal -23.63492890 0.375494092
## Poison -0.76271843 -0.036175345
## Psychic -6.41853421 0.006104521
## Rock 42.31312077 -0.458369507
## Steel 13.54121387 -0.220083655
## Water -8.16236939 0.025260963
##
## with conditional variances for "Type.1"
model2 = lmer(log(Attack) ~ log(Defense) + (1 + log(Defense) | Type.1), data = df)
summary(model2, corr=F)
## Linear mixed model fit by REML ['lmerMod']
## Formula: log(Attack) ~ log(Defense) + (1 + log(Defense) | Type.1)
## Data: df
##
## REML criterion at convergence: 727
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -6.7153 -0.5462 0.0248 0.6173 4.8005
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## Type.1 (Intercept) 0.75595 0.8695
## log(Defense) 0.04028 0.2007 -0.99
## Residual 0.13543 0.3680
## Number of obs: 800, groups: Type.1, 18
##
## Fixed effects:
## Estimate Std. Error t value
## (Intercept) 2.11558 0.26159 8.087
## log(Defense) 0.51944 0.06069 8.559