Skip to content

Instantly share code, notes, and snippets.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
library(lme4)
library(tidyverse)
library(broom)
data(sleepstudy, package='lme4')
df <- sleepstudy %>% as_data_frame()
df$Days <- scale(df$Days)
df
getwd()
setwd("/Users/conormcdonald/Desktop/")
model = MoonsModel(n_features=2, n_neurons=50)
cost_func = nn.BCELoss()
optimizer = tr.optim.Adam(params=model.parameters(), lr=0.01)
num_epochs = 20
losses = []
accs = []
for e in range(num_epochs):
class MoonsModel(nn.Module):
def __init__(self, n_features, n_neurons):
super(MoonsModel, self).__init__()
self.hidden = nn.Linear(in_features=n_features, out_features=n_neurons)
self.out_layer = nn.Linear(in_features=n_neurons, out_features=2)
def forward(self, X):
out = F.relu(self.hidden(X))
out = F.sigmoid(self.out_layer(out))
return out
class PrepareData(Dataset):
def __init__(self, X, y):
if not torch.is_tensor(X):
self.X = torch.from_numpy(X)
if not torch.is_tensor(y):
self.y = torch.from_numpy(y)
def __len__(self):
return len(self.X)
from sklearn.datasets import make_moons
import pandas as pd
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
import torch.nn.functional as F
import pandas as pd
import numpy as np
import os
import pymc3 as pm
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv("sleep_study.csv")
import pandas as pd
import numpy as np
import os
import pymc3 as pm
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
df = pd.read_csv("sleep_study.csv")
df.columns = df.columns.str.lower()
dayscaler = StandardScaler()
from sklearn.datasets import make_moons
import torch as tr
from torch.autograd import Variable
import torch.nn as nn
import matplotlib.pyplot as plt
import numpy as np
from keras.utils import np_utils
%matplotlib inline
# inspired by these tweets:
#The idea is very simple. You want to compare two groups' outcomes on some metric y.
#But the two groups are quite different in their covariates X.
#One approach is to predict y given X using a random forest,
#saving the proximity matrix. This i,jth entry in this matrix is the
#proportion of terminal nodes shared by observations i and j.
#That is, "how similar" they are in some random forest space.
#Now, for each person in group B, select the corresponding person in group A that is nearest in this this space.
#And compute your differences on this subset