Question

Does anybody have a code example of a "local linear regression model from scratch" in R?...

Does anybody have a code example of a "local linear regression model from scratch" in R? (ie, not using packages)?

I can't find any clues anywhere on this.

Thank you in advance!

Homework Answers

Answer #1

code

equire(data.table)
Happiness_Data=data.table(read.csv('2016.csv'))
colnames(Happiness_Data)<-gsub('.','',colnames(Happiness_Data),fixed=T)

###Data exploration

require(ggplot2)
require(GGally)
ggplot(dataPlot,aes(y=value,x=variable))+geom_violin()
ggpairs(Happiness_Data[,c(4,7:13),with=F],lower = list(continuous='smooth'))

require('rworldmap')
map.world <- map_data(map="world")
library(reshape2)
dataPlot<- melt(Happiness_Data,id.vars='Country', measure.vars=colnames(Happiness_Data)[c(4,7:13)])

#Add the data you want to map countries by to map.world
#In this example, I add lengths of country names plus some offset

dataPlot[Country=='United States',Country:='USA']
dataPlot[Country=='United Kingdoms',Country:='UK']

dataPlot[,value:=value/max(value),by=variable]
dataMap=data.table(merge(map.world,dataPlot,by.x='region',by.y='Country',all.x=T))
dataMap[is.na(variable),variable=unique(variable)]
dataMap=dataMap[order(order)]
gg <- ggplot()
gg <- gg + geom_map(data=dataMap, map=dataMap, aes(map_id=region,x=long,y=lat, fill=value))+facet_wrap(~variable,scale='free')
gg <- gg + scale_fill_gradient(low = "navy", high = "lightblue")
gg <- gg + coord_equal()
gg

##First model

model1<-lm(HappinessScore~EconomyGDPperCapita+Family+HealthLifeExpectancy+Freedom+TrustGovernmentCorruption,data=Happiness_Data)

##Quick summary

sum1=summary(model1)
sum1

##R²

sum1$r.squared*100

##Coefficients
sum1$coefficients
##p-value
df(sum1$fstatistic[1],sum1$fstatistic[2],sum1$fstatistic[3])
###Visualisation of residuals
ggplot(model1,aes(model1$residuals))+geom_histogram(bins=20,aes(y=..density..))+
geom_density(color='blue')+
geom_vline(xintercept = mean(model1$residuals),color='red')+
stat_function(fun=dnorm,
color="red",size=1,
args=list(mean=mean(model1$residuals),
sd=sd(model1$residuals)))
ggplot(model1,aes(model1$fitted.values,model1$residuals))+geom_point()+geom_hline(yintercept =
c(1.96*sd(model1$residuals),-1.96*sd(model1$residuals)),color='red')
##Fitted vs predicted
data.plot=data.frame('real'=Happiness_Data$HappinessScore,'fitted'=model1$fitted.values)
ggplot(data.plot,aes(x=real,y=fitted))+geom_point()+geom_smooth(method='lm')

##COnfidence interval
confint(model1,level = 0.95)
confint(model1,level = 0.99)
confint(model1,level = 0.90)
sum1$coefficients
##Standardized betas
std_betas=sum1$coefficients[-1,1]*data.table(model1$model)[,lapply(.SD,sd),.SDcols=2:6]/sd(model1$model$HappinessScore)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How do I code backward/forward selection in R with BIC as the criteria? Note: This is...
How do I code backward/forward selection in R with BIC as the criteria? Note: This is for multiple linear regression in which I am using this for variable selection. Please include any packages neccesary.
Consider the linear regression model ? = ? +?? + ? Suppose the variance of e...
Consider the linear regression model ? = ? +?? + ? Suppose the variance of e increases as X increases. What implications, if any, does this have for the OLS estimators and how would you proceed to estimate β in this case.
Please choose the most appropriate description of the concepts related to the linear model. The linear...
Please choose the most appropriate description of the concepts related to the linear model. The linear relation between two continuous variables Pearson's r asks the opposite question of this Type of plot that depicts the relation between X and Y Person's r has no error term in its formula Pearson's r can compare two sets of numbers when n1 does not equal n2 Prediction using the linear model The point on Y when X=0 The rate of change as one...
using r (include all code and packages used) Generate 25 variables, each of which consists of...
using r (include all code and packages used) Generate 25 variables, each of which consists of 25 random samples from a standard normal. Store these variables in a data frame – call it df.train – and randomly select one variable to be the response – rename it y. (The end result should be a data frame with 25 observations on 25 variables but with no relationships between any of the variables.) Repeat step (a) to create a test set called...
5. You have performed a simple linear regression model and ended up with Y(Y with a...
5. You have performed a simple linear regression model and ended up with Y(Y with a hat) = b0 + b1 x. (a) In your own words, describe clearly what the coefficient of determination, r^2, measures. (b) Suppose that your calculations produce r^2 = 0.215. As discussed in textbook, what can you conclude from this value? Furthermore, what can you say about the strength and direction of the relationship between the predictor and the response variable?
1. Consider an actual proof problem 2. The problem should estimate one multiple linear regression, or...
1. Consider an actual proof problem 2. The problem should estimate one multiple linear regression, or a logistic regression. Explain the estimation. 3. Consider a different model from step 2. You can try adding other random variables to explain your model, or consider different ways to explain (For example: probit model, lasso, trees, random forest, gradient boosting, or neural net). Then, compare the estimation results of the original model and the new model. ps. If you answer the question with...
If we have a multiple linear regression model: lm(life ~ male + birth + divo +...
If we have a multiple linear regression model: lm(life ~ male + birth + divo + beds + educ + inco, data = DATA) (1) What R command should we use to plot it standardized residuals against the FITTED values? (2) What R command should we use to compute and plot the leverage of each point and identify the points that have a leverage larger than 0.5? (3) What R command should we use to compute the Cook's distance for...
CW 2 List 5 assumptions of the simple linear regression model. You have estimated the following...
CW 2 List 5 assumptions of the simple linear regression model. You have estimated the following equation using OLS: ŷ = 33.75 + 1.45 MALE where y is annual income in thousands and MALE is an indicator variable such that it is 1 for males and 0 for females. a) According to this model, what is the average income for females? b) According to this model, what is the average income for females? c) What does OLS stand for? How...
r code Load the “star” data from the “faraway” package, and model “temp” using “light”. library(faraway)...
r code Load the “star” data from the “faraway” package, and model “temp” using “light”. library(faraway) data(star) fit = lm(temp ~ light, data = star) One may also suspect that the residuals (use all the data) follow a t distribution instead of normal. Using the same logic that we produced the QQ plot for comparing to the normal distribution, produce a QQ plot for comparing to a t distribution. You need to decide the degrees of freedom yourself (consider df...
II. You are given the following linear regression model, where ??2 represents a qualitative variable with...
II. You are given the following linear regression model, where ??2 represents a qualitative variable with two outcomes {0,1} and ??1 is a quantitative variable: ?? = ?0 + ?1??1 + ?2??2 + ?i 1. The response function for Y using ??2 = 0 is: (5pts) 2. The response function for Y using ??2 = 1 is: (5pts) 3. Explain theoretically what does a) ?1 indicates. (10pts) b) ?0 indicates (10pts) c) ?2 indicates (10pts)