Introduction to OpenMx

Slides:



Advertisements
Similar presentations
Structural Equation Modeling. What is SEM Swiss Army Knife of Statistics Can replicate virtually any model from “canned” stats packages (some limitations.
Advertisements

GENERAL LINEAR MODELS: Estimation algorithms
The General Linear Model. The Simple Linear Model Linear Regression.
Latent Growth Curve Modeling In Mplus:
Structural Equation Modeling
Classification and Prediction: Regression Via Gradient Descent Optimization Bamshad Mobasher DePaul University.
Multiple regression analysis
(Re)introduction to OpenMx Sarah Medland. Starting at the beginning  Opening R Gui – double click Unix/Terminal – type R  Closing R Gui – click on the.
(Re)introduction to Mx Sarah Medland. KiwiChinese Gooseberry.
Lesson #32 Simple Linear Regression. Regression is used to model and/or predict a variable; called the dependent variable, Y; based on one or more independent.
Longitudinal Modeling Nathan, Lindon & Mike LongitudinalTwinAnalysis_MatrixRawCon.R GenEpiHelperFunctions.R jepq.txt.
(Re)introduction to Mx. Starting at the beginning Data preparation Mx expects 1 line per case/family Almost limitless number of families and variables.
Basic Mathematics for Portfolio Management. Statistics Variables x, y, z Constants a, b Observations {x n, y n |n=1,…N} Mean.
Linear regression models in matrix terms. The regression function in matrix terms.
Simple Linear Regression Analysis
Matrix Approach to Simple Linear Regression KNNL – Chapter 5.
1 1 Slide Simple Linear Regression Chapter 14 BA 303 – Spring 2011.
Objectives of Multiple Regression
Karri Silventoinen University of Helsinki Osaka University.
Simple Linear Regression. Types of Regression Model Regression Models Simple (1 variable) LinearNon-Linear Multiple (2
© 2002 Prentice-Hall, Inc.Chap 14-1 Introduction to Multiple Regression Model.
Karri Silventoinen University of Helsinki Osaka University.
Lecture 3: Inference in Simple Linear Regression BMTRY 701 Biostatistical Methods II.
Introduction to OpenMx Sarah Medland. What is OpenMx? Free, Open-source, full–featured SEM package Software which runs on Windows, Mac OSX, and Linux.
Algebra 3: Section 5.5 Objectives of this Section Find the Sum and Difference of Two Matrices Find Scalar Multiples of a Matrix Find the Product of Two.
Statistical Methods Statistical Methods Descriptive Inferential
Univariate modeling Sarah Medland. Starting at the beginning… Data preparation – The algebra style used in Mx expects 1 line per case/family – (Almost)
Practical SCRIPT: F:\meike\2010\Multi_prac\MultivariateTwinAnalysis_MatrixRaw.r DATA: DHBQ_bs.dat.
Lesson Multiple Regression Models. Objectives Obtain the correlation matrix Use technology to find a multiple regression equation Interpret the.
Simple Linear Regression. The term linear regression implies that  Y|x is linearly related to x by the population regression equation  Y|x =  +  x.
Copyright © 2013, 2009, and 2007, Pearson Education, Inc. Chapter 13 Multiple Regression Section 13.3 Using Multiple Regression to Make Inferences.
Environmental Modeling Basic Testing Methods - Statistics III.
Longitudinal Modeling Nathan & Lindon Template_Developmental_Twin_Continuous_Matrix.R Template_Developmental_Twin_Ordinal_Matrix.R jepq.txt GenEpiHelperFunctions.R.
Statistics……revisited
1.3 Matrices and Matrix Operations. A matrix is a rectangular array of numbers. The numbers in the arry are called the Entries in the matrix. The size.
Linear Regression Linear Regression. Copyright © 2005 Brooks/Cole, a division of Thomson Learning, Inc. Purpose Understand Linear Regression. Use R functions.
Stats & Summary. The Woodbury Theorem where the inverses.
G Lecture 71 Revisiting Hierarchical Mixed Models A General Version of the Model Variance/Covariances of Two Kinds of Random Effects Parameter Estimation.
Welcome  Log on using the username and password you received at registration  Copy the folder: F:/sarah/mon-morning To your H drive.
MathematicalMarketing Slide 5.1 OLS Chapter 5: Ordinary Least Square Regression We will be discussing  The Linear Regression Model  Estimation of the.
Chapter 14 Introduction to Regression Analysis. Objectives Regression Analysis Uses of Regression Analysis Method of Least Squares Difference between.
QTL Mapping Using Mx Michael C Neale Virginia Institute for Psychiatric and Behavioral Genetics Virginia Commonwealth University.
MTH108 Business Math I Lecture 20.
Chapter 14 Introduction to Multiple Regression
BINARY LOGISTIC REGRESSION
Simple Linear Regression
Properties and Applications of Matrices
Measurement invariance in the linear factor model: practical
Correlation, Regression & Nested Models
Re-introduction to openMx
Simple Linear Regression - Introduction
Matrices Definition: A matrix is a rectangular array of numbers or symbolic elements In many applications, the rows of a matrix will represent individuals.
Univariate modeling Sarah Medland.
CHAPTER 29: Multiple Regression*
6-1 Introduction To Empirical Models
Multiple Regression Models
Section 2.4 Matrices.
(Re)introduction to Mx Sarah Medland
Longitudinal Modeling
Sarah Medland faculty/sarah/2018/Tuesday
(Virginia Commonwealth University)
Simple Linear Regression
Correlation and Regression
Including covariates in your model
BOULDER WORKSHOP STATISTICS REVIEWED: LIKELIHOOD MODELS
Matrices and Determinants
Andrea Friese, Silvia Artuso, Danai Laina
Autoregressive and Growth Curve Models
Correlation and Simple Linear Regression
Correlation and Simple Linear Regression
Presentation transcript:

Introduction to OpenMx Sarah Medland

What is OpenMx? • Free, Open-source, full–featured SEM package • Software which runs on Windows, Mac OSX, and Linux • A package in the R statistical programing environment • Two main approaches to writing OpenMx models – Path or Matrix Specification

• R is a functional language • Easy to define new functions • Items are stored as Objects • OpenMx uses functions to build objects • Arguments to the function have an order • Order can be changed by naming arguments

Matrices are the building blocks Matrix: a rectangular array of elements arranged in rows and columns. The order or dimension of a matrix is defined by the number of row and columns in the matrix. The order of a matrix is generally referred to as M x N (where M is the number of rows and N is the number of columns) Matrix A is a 3 x 3 matrix. Each element in the matrix is referred to by its placement in a row and column, where aij is the element in Matrix A in the ith row and jth column. Therefore, e is element a(2,2) Columns a d g b e h c f i A = (3x3) Rows

Matrices are the building blocks mxMatrix( type="Lower", nrow=nv, ncol=nv, free=TRUE, values=.6, label="a11", name="a" ), # additive genetic path coefficients Many types eg. type="Lower" Denoted by names eg. name="a“ Size eg. nrow=nv, ncol=nv Estimated parameters must be placed in a matrix & Mx must be told what type of matrix it is

Matrices are the building blocks mxMatrix( type=“Zero", nrow=2, ncol=3, name="a" ) mxMatrix( type=“Unit", nrow=2, ncol=3, name="a" ) mxMatrix( type=“Ident", nrow=3, ncol=3, name="a" )

Matrices are the building blocks mxMatrix( type=“Diag", nrow=3, ncol=3, free=TRUE, name="a" ) mxMatrix( type=“Sdiag", nrow=3, ncol=3, free=TRUE, name="a" ) mxMatrix( type=“Stand", nrow=3, ncol=3, free=TRUE, name="a" ) mxMatrix( type=“Symm", nrow=3, ncol=3, free=TRUE, name="a" ) mxMatrix( type=“Lower", nrow=3, ncol=3, free=TRUE, name="a" ) mxMatrix( type=“Full", nrow=2, ncol=4, free=TRUE, name="a" ) Many types

A model can have many matrices Equate parameters using labels Matrices inside Objects and build via this to trouble shoot

Matrix Operations + = = + = Matrix Addition and Subtraction: Undefined Matrices must be the same size If the matrices are of different orders, it is impossible to add them 2 1 3 5 6 4 8 7 2 9 6 2+4 1+8 3+7 5+2 6+9 2+6 6 9 10 7 15 8 + = = 2 1 3 5 6 + 4 7 9 8 2 6 = Undefined

Dot Product Also known as the element-wise product OpenMx symbol * A11B11 A12B12 A13B13 A21B21 A22B22 A23B23 A31B31 A32B32 A33B33 AB = = 

Matrix Multiplication (Star product) Number of columns of the first matrix must equal the number of rows of the second matrix. Product will have as many rows as the first matrix and as many columns as the second matrix. OpenMx symbol %*% C = A x B 2 1 3 5 6 3 4 7 5 6 1 C = x 60 37 34 60 37 34 60 37 60 3*2 + 4*3 + 7*6 3*1 + 4*5 + 7*2 5*2 + 6*3 + 1*6 5*1 +6*5 + 1*2 3*2 + 4*3 + 7*6 3*2 + 4*3 + 7*6 3*1 + 4*5 + 7*2 5*2 + 6*3 + 1*6 3*2 + 4*3 + 7*6 3*1 + 4*5 + 7*2 = =

Kroneker Product OpenMx symbol %x%

Quadratic Product The quadratic product is extremely useful in statistical analysis (particularly in Structural Equation Modeling) OpenMx symbol %&% 𝐴%&%𝐵=𝐴𝐵𝐴𝑇= 𝑎11 𝑎12 𝑎21 𝑎22 x 𝑏11 𝑏12 𝑏21 𝑏22 x 𝑎11 𝑎21 𝑎12 𝑎22

Our first OpenMx script Linear regression ( aka simple regression, Ordinary Least Squares Regression, linear model) Raw data Describe the relationship between two variables, X and Y, as a straight line The regression of BMI on variable Age Does age predict BMI? BMI=weight in kg/ (height in m)2

Linear Regression The regression of variable Y on variable X is given by: 𝑦 𝑖 = 𝛽 0 + 𝛽 1 𝑥 𝑖 + 𝜀 𝑖 𝑖=1,…,𝑛 where: 𝛽 0 (Intercept) value of y when x=0 𝛽 1 (Slope) increase in Y for each unit change in X 𝜀 𝑖 (Random Error) 𝜀 𝑖 ~𝑁(0, 𝜎 2 ) Linear Function: 𝛽 0 + 𝛽 1 𝑥 𝑖 = E ( Y | X = 𝑥 𝑖 )

As a path diagram

Starting at the beginning… Data preparation The algebra style used in OpenMx expects 1 line per case/family (Almost) limitless number of families and variables Data needs to be read into R before it can be analysed (the commands to read the data can be nested within the R script) Default missing code is now NA

Saving your output Numerous options Saving workspace most common but could potentially lead to IRB issues Sink and history both miss sections Today we will use the teaching demos package records everything that goes in and comes out require (TeachingDemos) txtStart(file="linearRegression.omx", commands=TRUE, results=TRUE) txtStop()

Getting your data into R Open RStudio Example data:ozbmi2.txt OZbmi<-read.table("ozbmi2.txt", header=T, na.strings = "NA") head(data) # using subset function create new dataset without missing data OZbmi <- subset(data, age !="NA" , select=c(bmi1, age))

Regression using lm BMIfit <- lm(bmi1 ~ age, data=OZbmi) summary(BMIfit) # show results coefficients(BMIfit) # model coefficients

Regression using OpenMx Model contains: 4 matrices and 3 estimated parameters BMI Free observed variable 𝛽 0 and 𝜎2 𝜀 are estimated Age Fixed observed variable Regression of BMI on Age 𝛽 1 is estimated

Regression using OpenMx # Variance/Covariance matrix Variance <- mxMatrix( type="Full", nrow=1, ncol=1, free=TRUE, values=11, labels='resid', name="residualVar" )

Regression using OpenMx require (OpenMx) depVar <- 'bmi1‘ # Variance/Covariance matrix Variance <- mxMatrix( type="Full", nrow=1, ncol=1, free=TRUE, values=11, labels='resid', name="residualVar" ) # Regression betas b0 <-mxMatrix(type="Full", nrow=1, ncol=1, free=T, values=22, labels="beta0", name="Intercept" ) b1 <-mxMatrix(type="Full", nrow=1, ncol=1, free=T, values=0, labels="beta1", name="bAge" ) # Independent variable x <-mxMatrix(type="Full", nrow=1, ncol=1, free=F, labels="data.age", name="Age" )

# Building the model ( 𝑦 𝑖 = 𝛽 0 + 𝛽 1 𝑥 𝑖 ) expMean <- mxAlgebra(intercept + bAge%x%Age, name="regress") # Specify the data regData <- mxData( observed=OZbmi, type="raw" ) inclusions <- list(Variance, b0, b1, bAge, expMean)

# The dimnames map the data to the model. # Finally, we call up the results of the algebras as the arguments for the expectation function. # The dimnames map the data to the model. # NOTE- The matrix names defined in mxMatrix() statements are used here NOT the objects that are used to store the matrices. exp <- mxExpectationNormal( covariance="residualVar", means="regress", dimnames=depVar )

#The fit function declares that the model is fit using maximum likelihood. # When combined with raw data this means full information maximum likelihood (FIML) is optimized. funML <- mxFitFunctionML()

#Build the model - specify the name of the model, the objects referenced, the data and the objective regModel <- mxModel( "Regression101", inclusions, regData, exp, funML ) # Run the model & summarize output regFit <- mxRun( regModel, intervals=FALSE ) regSum <- summary( regFit )

Intuitive Logic of Optimization Start with an arbitrary set of initial parameters values. (Starting Values) Determine a direction of movement for the parameters (larger or smaller) Determine a step length to move (how much larger or smaller) Rinse and repeat until some termination criteria is reached and then stop.

Maximum Likelihood Estimation Let us assume that X is an i.i.d. random variable with a probability of p(x|θ) = P(X=x), where θ is a parameter We know that if observations are independent, then the joint probability of their occurrence is the product of the individual probabilities. Hence:

# ReRunning to look at optimization #Looking at the optimization process # ReRunning to look at optimization regModel <- mxOption(regModel,"Checkpoint Units", "iterations") regModel <- mxOption(regModel,"Checkpoint Count", 1) regFit <- mxRun( regModel, intervals=FALSE, checkpoint=T )

OpenMx vs lm OpenMx regSum$parameters Lm

Is beta1 different from 0? # Go back and pickup the model so that we can run significance tests ageEfModel <- regFit #set beta 1 to 0 ageEfModel <- omxSetParameters( ageEfModel, label="beta1", free=FALSE, values=0 ) ageEfFit <- mxRun(ageEfModel, intervals=FALSE) (ageEfSumm <- summary(ageEfFit))

# difference in fit deltaLL <-ageEfSumm$Minus2LogLikelihood - regSum$Minus2LogLikelihood # difference in df deltaDF <-ageEfSumm$degreesOfFreedom - regSum$degreesOfFreedom # significance test pchisq(deltaLL, lower.tail=F, deltaDF)

Bus shelter on the road to Sintra (Portugal) Questions? Bus shelter on the road to Sintra (Portugal)