{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Discussion 08 - Masanao Yajima imptab< function xv rw cl...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Masanao Yajima imptab <- function( xv, rw, cl, eps = 1e-6, itmax = 100, verbose = TRUE ){ l <- tapply( xv, list( rw, cl ), length ); l <- ifelse( is.na( l ), 0, l ); lmax <- max( l ); xsum <- tapply( xv, list( rw, cl ), sum); xsum <- ifelse( is.na( xsum ), 0, xsum ); z <- xsum / lmax; sigold <- sum( xv^2 ); itel <- 1; repeat { mu <- mean( z ); alpha <- apply( z, 1, mean ) - mu; beta <- apply( z, 2, mean ) - mu; xhat <- mu + alpha[rw] + beta[cl]; signew <- sum( ( xv - xhat )^2 ) if( verbose ){ cat("Iteration: ",formatC( itel, digits = 3, width = 3 ), "Old Loss: ", formatC( sigold, digits = 6, width = 10, format = "f" ), "New Loss: ", formatC( signew, digits = 6, width = 10, format = "f" ), "¥n") } z <- ( xsum + ( lmax - l ) * ( mu + outer( alpha, beta, "+" ) ) ) / lmax if( ( abs( sigold - signew ) < eps ) || ( itel == itmax ) ){ break()} sigold <- signew; itel <- itel + 1; } return( list( mu = mu, alpha = alpha, beta = beta ) ) } # Generate data set.seed( 11111 ) dat <- data.frame( rw = as.factor( sample( 1:5, 100, replace = TRUE ) ), cl = as.factor( sample( 1:5, 100, replace = TRUE ) ), xv = rnorm( 100 ) ) attach( dat )...
View Full Document

{[ snackBarMessage ]}

Page1 / 16

Discussion 08 - Masanao Yajima imptab< function xv rw cl...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online