Você está na página 1de 38

Motivation Model Overview Data R/Shiny application Conclusion

Applying historical context to home value estimates

David J. Moore, Ph.D.

California Steate University, Sacramento


College of Business Administration

May 4, 2017
Motivation Model Overview Data R/Shiny application Conclusion

Introduction

Home purchase decisions can be emotional.


Feelings are not reliable guides to reality. -Robert Wright.
HomeValuatorTM , an interactive R/Shiny application that I
developed, can be a less unreliable guide to reality than
feelings.
R is a very flexible (and free) package useful in cleaning,
formatting, processing, and presenting messy publicly available
data.
Shiny provides a straightforward interactive R interface.
Shiny is useful in scenarios where one wants to adjust multiple
inputs and examine the response of multiple outputs.
Motivation Model Overview Data R/Shiny application Conclusion

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Motivation

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Motivation

Motivation 1/3

I know someone who bought a property in 2008 feeling the


market had bottomed out.
Unfortunately the feeling was evidence-free (i.e.,
HomeValuatorTM -free).
Appraisal was a gross over-estimate that fed the feeling.
That person lost money.
Home prices and apartment rents must be related
(cointegrated).
If rent is relatively high buy a house.
If home prices are relatively high rent an apartment.
There are many other factors that influence the rent vs. buy
decision: interest rates, unemployment, population growth,
demographic trends, etc.
All of that information is embedded in home prices and rental
rates.
Motivation Model Overview Data R/Shiny application Conclusion

Motivation

Motivation 2/3
Sacramento Bee 2017.04.28:

Home sales are best


in 5 years.
We heard that
before... in 2007.
Does best mean
time to buy or time to
sell?
Motivation Model Overview Data R/Shiny application Conclusion

Motivation

Motivation 3/3
Motivation Model Overview Data R/Shiny application Conclusion

De-motivation

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

De-motivation

Do home buyers really care?

Bought at height of the


CNBC 2014.12.30 Renters housing boom in 2007.
shelled out $441 billion in 2014: Lost her home to foreclosure
Time to buy? in 2011.
Buying again in 2014.
Doesnt want to throw
money away renting.
She just finished throwing
away money and making
someone else rich.
Having her father move in
because she really cant
afford the house?
Motivation Model Overview Data R/Shiny application Conclusion

De-motivation

Do agents and banks care?

End-users (as in previous slide): follow emotions rather than


numbers.
Real estate agents: your number can be a liability for me.
Banks: will collect origination fees then sell the loan anyway.
Making someone else rich agents and banks collect fees
with transactions.
Its always a good time to buy because those making that
statement make money on transactions.
Motivation Model Overview Data R/Shiny application Conclusion

Re-motivation

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Re-motivation

The resurrection...

Intended to present at SSA for years, thought this would be a


good topic.
Banks may use my number to determine which loans to sell,
keep, and buy.
I might be able to use the algorithms to value underlying assets
of REITS a minimal human contact path to monetization.
I may buy a home in a few years and it will be nice to keep the
app fresh.
Motivation Model Overview Data R/Shiny application Conclusion

Brief model overview

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Brief model overview

Model Overview
Appraisal HomeValuatorTM
Based on 20+ years of
Based on 1-3 months sales
sales, rent, and other
data
data.
Value adjusted by deviation Value derived from market
from cross-sectional rent, a number that includes
averages home characteristics
HomeValuatorTM
Takes advantage of home price - apartment rent relationship.
Takes advantage of the information content in market rent
numbers.
Produces an emotionless cap-rate based valuation estimate.

Rent Costs
V =
Cap Rate
Motivation Model Overview Data R/Shiny application Conclusion

Price

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Price

Price data

Obtained from California Association of Realtors website.


Motivation Model Overview Data R/Shiny application Conclusion

Price

ingressPrice.R - code snippet

###########################
# Read i n C a l i f o r n i a d a t a #
###########################
x < r e a d . c s v ( " i n / P r i c e C A 2 0 1 6 1 1 3 0 . c s v " , h e a d e r=TRUE, f i l l =
, TRUE, s t r i n g s A s F a c t o r s = FALSE )
x < m e l t ( x , i d . v a r s=c ( yyyymm ) , v a r i a b l e _ n a m e = c o u n t y )
x $ s t a t e < "CA"
x$Date < a s . yearmon ( a s . c h a r a c t e r ( x$yyyymm ) , %Y%m )

# Replace . i n county with space


x $ c o u n t y < g s u b ( \ \ . , , x $ c o u n t y )

# Remove d o l l a r s i g n s and commas


x $ v a l u e < r m w h i t e s p a c e ( x $ v a l u e )
x $ v a l u e < g s u b ( \ \ $ | , , , x $ v a l u e )
x $ M e d i a n P r i c e < a s . n u m e r i c ( x $ v a l u e )

# Drop u n u s e d v a r i a b l e s
m y v a r s < c ( " Date " , " s t a t e " , " c o u n t y " , " M e d i a n P r i c e " )
x < na . o m i t ( x [ , m y v a r s ] )
Motivation Model Overview Data R/Shiny application Conclusion

Price

ingressPrice.R - output snippet


Motivation Model Overview Data R/Shiny application Conclusion

Rent data

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Rent data

Rent data

Obtained from U.S. Department of Housing and Urban


Development.

Note the odd formating of fmrxx_y where xx= two-digit year and
y=number of bedrooms.
Motivation Model Overview Data R/Shiny application Conclusion

Rent data

ingressRent.R - code snippet 1/3


# Read i n F a i r Market Rent d a t a from HUD
r e n t < r e a d . c s v ( " i n /FMR_All_1983_2017 . c s v " , h e a d e r=TRUE,
, f i l l =TRUE, s t r i n g s A s F a c t o r s = FALSE )
r e n t $ s t a t e c d < r e n t $ s t a t e
r e n t $ s t a t e < NULL

# Read i n F e d e r a l I n f o r m a t i o n P r o c e s s i n g S t a n d a r d s t o o b t a i n
# s t a t e c d <> s t a t e two l e t t e r a b b r e v i a t i o n t a b l e
f i p s < r e a d . c s v ( " i n / f i p s . c s v " , h e a d e r=TRUE, f i l l =TRUE,
, s t r i n g s A s F a c t o r s = FALSE )
f i p s < f i p s [ , c ( 2 , 3 ) ] # a l l I c a r e a b o u t i s t h e s t a t e and
, s t a t e c d

# Add s t a t e name t o r e n t d a t a
r e n t < merge ( r e n t , f i p s , by = s t a t e c d )

# Remove nonA S C I I ( m u l t i b y t e ) c h a r a c t e r s from pmsaname


E n c o d i n g ( r e n t $ c n t y n a m e ) < " l a t i n 1 " # s i n c e t h o s e nonA S C I I
, c h a r a c t e r s e x i s t
r e n t $ c n t y n a m e < i c o n v ( r e n t $ c n t y n a m e , l a t i n 1 , ASCII , s u b
, = )
Motivation Model Overview Data R/Shiny application Conclusion

Rent data

ingressRent.R - code snippet 2/3

# E x t r a c t County name
r e n t $ c o u n t y < s t r _ r e p l a c e ( r e n t $ c n t y n a m e , " County " , " " )
r e n t $ c n t y n a m e < NULL # no l o n g e r n e e d e d

# C r e a t e l i s t o f v a r i a b l e s t h a t c o n t a i n fmr
f m r v a r s < g r e p ( fmr , c o l n a m e s ( r e n t ) , v a l u e=TRUE)

# R e t a i n o n l y v a r i a b l e s t h a t c o n t a i n _
f m r v a r s < g r e p ( _ , f m r v a r s , v a l u e=TRUE)

# Drop v a r i a b l e s t h a t c o n t a i n a r e a
f m r v a r s < f m r v a r s [ 1 : ( g r e p ( a r e a , f m r v a r s ) 1) ]
r e n t < r e n t [ , c ( " s t a t e " , " c o u n t y " , f m r v a r s ) ]

# Transpose ( melt ) data


r e n t < m e l t ( r e n t , i d . v a r s=c ( s t a t e , c o u n t y ) , v a r =
, year_bed )

# Sort data
r e n t < r e n t [ o r d e r ( r e n t $ s t a t e , r e n t $ c o u n t y ) , ]
Motivation Model Overview Data R/Shiny application Conclusion

Rent data

ingressRent.R - code snippet 3/3

# C o n v e r t year_bed from f a c t o r t o c h a r a c t e r
r e n t $ y e a r _ b e d < a s . c h a r a c t e r ( r e n t $ y e a r _ b e d )

# C r e a t e bedroom v a r i a b l e
r e n t $ b e d r o o m s < p a s t e ( " Rent " , s u b s t r ( r e n t $ y e a r _ b e d , 7 , 7 ) ,
, s e p ="")

# Create four d i g i t year v a r i a b l e


t w o d i g i t < f u n c t i o n ( x ) { a s . i n t e g e r ( s u b s t r ( x , 4 , 5 ) ) }
r e n t $ y e a r < i f e l s e ( t w o d i g i t ( r e n t $ y e a r _ b e d ) <68 , 2000+
, t w o d i g i t ( r e n t $ y e a r _ b e d ) , 1900+ t w o d i g i t ( r e n t $ y e a r _ b e d )
, )
r e n t $ y e a r _ b e d < NULL # don t n e e d t h i s anymore

# Sort r e n t data frame


r e n t < r e n t [ o r d e r ( r e n t $ y e a r , r e n t $ s t a t e , r e n t $ c o u n t y ,
, r e n t $ b e d r o o m s ) , ]

# Drop o b s e r v a t i o n s w i t h m i s s i n g d a t a
r e n t < na . o m i t ( r e n t )
Motivation Model Overview Data R/Shiny application Conclusion

Rent data

ingressRent.R - output snippet

# Cast data again . Have t o u s e mean ( ) f u n c t i o n s i n c e some


, c o u n t i e s h a v e m u l t i p l e l i t t l e towns
# e . g . , Aroostook
r e n t < c a s t ( d a t a=r e n t , f o r m u l a=y e a r+s t a t e+c o u n t y ~bedrooms ,
, f u n . a g g r e g a t e=mean , na . rm=T)
Motivation Model Overview Data R/Shiny application Conclusion

Other data

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Other data

Other data

Operational cost data: American Housing Survey, National


Association of Housing Builders.
Interest rates: Federal Reserve Economic Data
Current market rent estimate for given home: real estate
agent, homeowner, zillow, rentometer.com
Motivation Model Overview Data R/Shiny application Conclusion

Shiny overview

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Shiny overview

Shiny overview

Shiny is an interactive R front-end developed by the folks from


R-Studio.
Shiny can run locally (Ill do this today), on a R Studio Server
(browser-based version of R Studio), or on a Shiny Server (web
page for Shiny app).
Three main files for a Shiny application:
1 global.R - executed once at startup. Useful for loading
necessary packages, defining global variables, reading in static
data file, etc.
2 ui.R - define the user interface buttons, sliders, graphics, etc.
3 server.R - code that responds to input changes from ui.R. This
contains all of the heavy lifting computations.
Free for one user, must pay to allow multiple user access.
Possible workaround in future enhancements slide.
Motivation Model Overview Data R/Shiny application Conclusion

Shiny code snippets

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Shiny code snippets

global.R - Data read and variable definition


require ( shiny )
require ( stringr )

r e n t < r e a d . c s v ( " o u t / R e n t 2 0 1 6 1 2 3 1 . c s v " , h e a d e r=TRUE, f i l l =


, TRUE, s t r i n g s A s F a c t o r s = FALSE )
p r i c e < r e a d . c s v ( " o u t / P r i c e 2 0 1 6 1 1 3 0 . c s v " , h e a d e r=TRUE, f i l l
, =TRUE, s t r i n g s A s F a c t o r s = FALSE )

# Obtain i n i t i a l l i s t of c o u n t i e s #
d e f a u l t S t a t e < "CA"
d e f a u l t C o u n t y < " S a c r a m e n t o "
C o u n t i e s < u n i q u e ( s u b s e t ( r e n t , s t a t e==d e f a u l t S t a t e ) [ , "
, c o u n t y " ] )

# P r i c e and r e n t s o u r c e d a t a s t r i n g s a r e NOT f a c t o r s
# R a t h e r , t h e y a r e c o u n t y names .
o p t i o n s ( s t r i n g s A s F a c t o r s=FALSE )

# Miscellaneous global variables


C u r r e n t Y e a r < a s . n u m e r i c ( f o r m a t ( S y s . Date ( ) , "%Y" ) )
rhoAvg < 0 . 0 2 1 3
Motivation Model Overview Data R/Shiny application Conclusion

Shiny code snippets

ui.R - User interface


shinyUI ( fluidPage (
# Application t i t l e
h3 ( " E f f i c i e n t Minds Home V a l u a t o r 12 b e t a " ) ,

# S i d e b a r w i t h a s l i d e r i n p u t f o r number o f b i n s
s i d e b a r L a y o u t ( p o s i t i o n =" r i g h t " ,
sidebarPanel (
inputPanel (
s e l e c t I n p u t ( " S t a t e " , l a b e l =" S t a t e : " ,
c h o i c e s=S t a t e s ,
s e l e c t e d=d e f a u l t S t a t e ) ,
s e l e c t I n p u t ( " County " , l a b e l ="County : " ,
c h o i c e s=C o u n t i e s ,
s e l e c t e d=d e f a u l t C o u n t y ) ,

s e l e c t I n p u t (" PropertyType " , l a b e l = " PropertyType : " ,


c h o i c e s=c ( " S i n g l e F a m i l y " , " Condo " ) ,
s e l e c t e d =" S i n g l e F a m i l y " ) ,

s l i d e r I n p u t ( " Y e a r B u i l t " , l a b e l = " Year b u i l t : " , s e p ="" ,


min = 1 9 1 9 , max = C u r r e n t Y e a r , v a l u e = 2 0 1 6 , s t e p = 1 ) ,
Motivation Model Overview Data R/Shiny application Conclusion

Shiny code snippets

server.R - Computations

# Define global r e a c t i v e v a r i a b l e s
v a l u e s < r e a c t i v e V a l u e s ( )
v < r e a c t i v e V a l u e s ( )
v $ C o u n t i e s < C o u n t i e s
v a l u e s $ P 2 R a v g < 0

# Formatting function
EMfmt < f u n c t i o n ( x ) f o r m a t ( r o u n d ( x , 0 ) , s c i e n t i f i c =FALSE ,
, b i g . mark =" ,")

s h i n y S e r v e r ( f u n c t i o n ( input , output , s e s s i o n ) {

o u t p u t $ d i s t P l o t < r e n d e r P l o t ( {

# Create l i s t of unique counties #


C o u n t i e s < u n i q u e ( s u b s e t ( r e n t , s t a t e==i n p u t $ S t a t e ) [ , " c o u n t y
, " ] )
Motivation Model Overview Data R/Shiny application Conclusion

Application demo

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Application demo

Time for a demo

Demo using local R Studio with Shiny...


Motivation Model Overview Data R/Shiny application Conclusion

Conclusion and future enhancements

Outline
1 Motivation
Motivation
De-motivation
Re-motivation
2 Model Overview
Brief model overview
3 Data
Price
Rent data
Other data
4 R/Shiny application
Shiny overview
Shiny code snippets
Application demo
5 Conclusion
Conclusion and future enhancements
Motivation Model Overview Data R/Shiny application Conclusion

Conclusion and future enhancements

Conclusion

Home purchase decisions can be emotional.


Feelings are not reliable guides to reality. - Robert Wright.
HomeValuatorTM , an interactive R/Shiny application that I
developed, can be a less unreliable guide to reality than
feelings.
R is a very flexible (and free) package useful in cleaning,
formatting, processing, and presenting messy publicly available
data.
Shiny provides a straightforward interactive R interface.
Shiny is useful in scenarios where one wants to adjust multiple
inputs and examine the response of multiple outputs.
Motivation Model Overview Data R/Shiny application Conclusion

Conclusion and future enhancements

Future enhancements

Use zipcode-level Zillow data rather than county-level CAR


and HUD data.
Alternative deployment paths
Load pre-processed data into Google BigQuery and access via
R-SQL connection using Shiny server or DeployR.
Google Data Studio: Simple solutions help you cleanse,
combine, and transform all your data without writing code or
creating SQL queries. Build engaging reports and
dashboards.
More detailed analysis
Include geohash data for heat maps and ability to provide
real-time value estimates of a home while you are inside.
Weave in additional data such as unemployment, median
salaries, population growth, etc.
Extra data + Big Query + Machine Learning = refined
forecasts
Motivation Model Overview Data R/Shiny application Conclusion

Conclusion and future enhancements

Contact information

David J. Moore, Ph.D.


http://www.efficientminds.com
David@efficientminds.com