Sie sind auf Seite 1von 7

public class LoanApplication

{
private DateTime _loanApplicationDate;
private Double _downpaymentAmount;
private Double _totalFinancedMortgageAmount;
private List<Applicant> _applicants = new List<Applicant>();
private MortgageProperty _home;
private Boolean _pmiRequired;
/// <summary>
/// The date of the loan application.
/// </summary>
public DateTime LoanApplicationDate
{
get { return _loanApplicationDate; }
set { _loanApplicationDate = value; }
}
/// <summary>
/// The amount the applicants are putting down on the
/// purchase of the property.
/// </summary>
public Double DownPaymentAmount
{
get { return _downpaymentAmount; }
set { _downpaymentAmount = value; }
}
/// <summary>
/// The total amount being financed by the mortgage.
/// </summary>
public Double TotalFinancedMortgageAmount
{
get { return _totalFinancedMortgageAmount; }
set { _totalFinancedMortgageAmount = value; }
}
/// <summary>
/// The list of applicants on this mortgage application.
/// </summary>
public List<Applicant> Applicants
{
get { return _applicants; }
set { _applicants = value; }
}
/// <summary>
/// The home being financed by the mortgage.
/// </summary>
public MortgageProperty Home
{
get { return _home; }

set { _home = value; }


}
/// <summary>
/// Indicator that pmi is required for this loan.
/// </summary>
public Boolean PmiRequired
{
get { return _pmiRequired; }
set { _pmiRequired = value; }
}
}
}

{
/// <summary>
/// Business object that represents a mortgage loan property.
/// </summary>
public class MortgageProperty
{
private String _address;
private String _city;
private String _state;
private String _zip;
private Boolean _isInGoodCondition;
private DateTime _yearBuilt;
private DateTime _roofReplacementDate;
private DateTime _sidingReplacementDate;
private DateTime _electricalUpdateDate;
private DateTime _plumbingUpdateDate;
private DateTime _appraisalDate;
private Double _appraisalAmount;
private Boolean _passedInspection;
private Double _purchasePrice;
/// <summary>
/// Property street address.
/// </summary>
public String Address
{
get { return _address; }
set { _address = value; }
}
/// <summary>
/// Property city.
/// </summary>
public String City
{
get { return _city; }
set { _city = value; }
}
/// <summary>
/// Property state code.
/// </summary>
public String State
{
get { return _state; }
set { _state = value; }
}
/// <summary>
/// Property zip code.
/// </summary>

public String Zip


{
get { return _zip; }
set { _zip = value; }
}
/// <summary>
/// Indicates if the property is in good condition.
/// </summary>
public Boolean IsInGoodCondition
{
get { return _isInGoodCondition; }
set { _isInGoodCondition = value; }
}
/// <summary>
/// The year the home was built.
/// </summary>
public DateTime YearBuilt
{
get { return _yearBuilt; }
set { _yearBuilt = value; }
}
/// <summary>
/// The date of the last time the roof replaced.
/// </summary>
public DateTime RoofReplacementDate
{
get { return _roofReplacementDate; }
set { _roofReplacementDate = value; }
}
/// <summary>
/// The date of the last time the siding was replaced.
/// </summary>
public DateTime SidingReplacementDate
{
get { return _sidingReplacementDate; }
set { _sidingReplacementDate = value; }
}
/// <summary>
/// The date of the last time the electrical was updated or replaced.
/// </summary>
public DateTime ElectricalUpdateDate
{
get { return _electricalUpdateDate; }
set { _electricalUpdateDate = value; }
}
/// <summary>
/// The date of the last time the plumbing was updated.

/// </summary>
public DateTime PlumbingUpdateDate
{
get { return _plumbingUpdateDate; }
set { _plumbingUpdateDate = value; }
}
/// <summary>
/// The date the property was appraised.
/// </summary>
public DateTime AppraisalDate
{
get { return _appraisalDate; }
set { _appraisalDate = value; }
}
/// <summary>
/// The amount the property was appraised at by the bank.
/// </summary>
public Double AppraisalAmount
{
get { return _appraisalAmount; }
set { _appraisalAmount = value; }
}
/// <summary>
/// The indicator that property passed or failed its appraisal inspection.
/// </summary>
public Boolean PassedInspection
{
get { return _passedInspection; }
set { _passedInspection = value; }
}
/// <summary>
/// The purchase price of the property.
/// </summary>
public Double PurchasePrice
{
get { return _purchasePrice; }
set { _purchasePrice = value; }
}
}
}

flip = function(p) runif(1) < p


wetGrass.sim = function(i) {
flip = function(p) runif(1) < p
cloudy = flip(.5)
sprinkler = cloudy && flip(.1)
rain = cloudy && flip(.8)
wet.Grass = rain && flip(.9)||sprinkler && flip(.9)||flip(.0)
return(c(cloudy = cloudy, sprinkler = sprinkler, rain = rain, wet.Grass = wet.Grass))
}
n.runs = 200
dag = t(sapply(1:n.runs, FUN=wetGrass.sim))
head(dag)
query = function(variable.name, n.sims = 200) {
sims = sapply(1:n.sims, FUN=wetGrass.sim)
proportion = length(which(sims[variable.name,] == TRUE))/n.sims
return(proportion)
}
conditional.query = function(query.variable, conditioners, n.sims=10){
samples = c()
while(length(samples) < n.sims){
cloudy = flip(.5)
sprinkler = cloudy && flip(.1)
rain = cloudy && flip(.8)
wet.Grass = rain && flip(.9)||sprinkler && flip(.9)||flip(.0)
truth.values = c(cloudy = cloudy, sprinkler = sprinkler, rain = rain, wet.Grass = wet.Grass)
query.index = which(query.variable == names(truth.values))
conditioner.indices = which(names(truth.values) %in% conditioners)
if (all(truth.values[conditioner.indices] == T)) {
samples = c(samples, truth.values[query.index])
}
}
return (length(which(samples ==T))/n.sims)
}

library(ISLR)
attach(Carseats)
head(Carseats)
###Start Data Manipulation
range(Sales) #Sales range from 0 to 16
# create a categorical variable based on Sales
High = ifelse(Sales >=8, "Yes", "No")
#appends High to Carseat dataset, and now our dataset is ready!
Carseats = data.frame(Carseats, High)
Carseats[,-1]
###Split data into testing and training using
set.seed(2)
train = sample(1:nrow(Carseats), nrow(Carseats)/2)
test = -train
training_data = Carseats[train,]
testing_data = Carseats[test,]
testing_High = High[test]
#fit the tree model using training data
tree_model = tree(High~., training_data)
plot(tree_model)
text(tree_model, pretty = 0)
#check how the model is doing using the test data
tree_pred = predict(tree_model, testing_data, type = "class")
mean(tree_pred != testing_High)
### Prune the tree
##Cross validation to check where to stop pruning
set.seed(3)
cv_tree = cv.tree(tree_model, FUN = prune.misclass)
names(cv_tree)
plot(cv_tree$size, cv_tree$dev, type = "b")
###prune the tree
pruned_model = prune.misclass(tree_model, best = 9)
plot(pruned_model)
text(pruned_model, pretty = 0)
### check how it is doing
tree_pred = predict(pruned_model, testing_data, type = "class")
mean(tree_pred != testing_High)

Das könnte Ihnen auch gefallen